ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的测验 > Excel VBA > 正则练习题(一)

正则练习题(一)

作者:绿色风 分类: 时间:2022-08-18 浏览:126
楼主
kevinchengcw
以下内容是我从网上得到的文本经常遇到的情况:
  1. Four Cheese Ravioli
  2. Ingredients
  3. Ravioli Dough: 2 cups all-purpose flour 1 pinch salt 1 teaspoon olive oil 2 eggs 1 1/2 tablespoons water
  4. Ravioli Filling: 1 (8 ounce) container ricotta cheese 1 (4 ounce) package cream cheese, softened 1/2 cup shredded mozzarella cheese 1/2 cup provolone cheese, shredded 1 egg 1 1/2 teaspoons dried parsley
  5. Pesto-Alfredo Cream Sauce: 2 tablespoons olive oil 2 cloves garlic, crushed 3 tablespoons prepared basil pesto
  6. sauce 2 cups heavy cream 1/4 cup grated Parmesan cheese 1 (24 ounce) jar marinara sauce
  7. Egg Wash: 1 egg 1 tablespoon water
请用正则将文本变成下面这样:
  1. Four Cheese Ravioli
  2. Ingredients
  3. Ravioli Dough:
  4. 2 cups all-purpose flour
  5. 1 pinch salt
  6. 1 teaspoon olive oil
  7. 2 eggs
  8. 1 1/2 tablespoons water
  9. Ravioli Filling:
  10. 1 (8 ounce) container ricotta cheese
  11. 1 (4 ounce) package cream cheese, softened
  12. 1/2 cup shredded mozzarella cheese
  13. 1/2 cup provolone cheese, shredded
  14. 1 egg
  15. 1 1/2 teaspoons dried parsley
  16. Pesto-Alfredo Cream Sauce:
  17. 2 tablespoons olive oil
  18. 2 cloves garlic, crushed
  19. 3 tablespoons prepared basil pesto sauce
  20. 2 cups heavy cream
  21. 1/4 cup grated Parmesan cheese
  22. 1 (24 ounce) jar marinara sauce
  23. Egg Wash:
  24. 1 egg
  25. 1 tablespoon water
注意:正则表达式须对此类型通用。

答题有效期15天。

2010-12-18 开帖:
参考答案:
  1. Sub test1()
  2. Dim RegEx As Object, N&, Str$
  3. Set RegEx = CreateObject("vbscript.regexp")
  4. With RegEx
  5.     .Global = True
  6.     .Pattern = "([^\d/(])([\d/\.]+ [^/])"  
  7. End With
  8. For N = 1 To Cells(Rows.Count, 1).End(3).Row
  9.     Cells(N, 2) = RegEx.Replace(Cells(N, 1).Value, "$1" & vbNewLine & "$2")
  10. Next N
  11. Set RegEx = Nothing
  12. End Sub

 
2楼
liuguansky

正则练习.rar

还没人回帖啊,我回一个吧.
  1. Sub justtest()
  2.   Dim Regex As Object, str1$, mat
  3.   Set Regex = CreateObject("vbscript.regexp")
  4.   With Regex
  5.     .Global = True
  6.     .MultiLine = True
  7.     .Pattern = "([1-9]+(\s[1-9]+\/[1-9]+|\/[1-9]+|\s\([1-9]+\s\w+\))?)" '(\s\w+)+?)(?=[1-9])"
  8.     str1 = Cells(1, 1).Value
  9.     Cells(1, 2) = .Replace(str1, vbCrLf & "$1")
  10.     'Debug.Print .Replace(str1, vbCrLf & "$1")
  11.   End With
  12.   End Sub
3楼
xyh9999
k哥,你的原文件有一处多了个换行符,请检查一下。
xyh9999Test.rar



  1. Function xyh9999k()
  2. cc = LqcFileToStr(ThisWorkbook.Path & "\k.txt")
  3. Set RegEx = CreateObject("VBscript.RegExp")
  4. With RegEx
  5.     .MultiLine = False
  6.     .Global = True
  7.     .IgnoreCase = True
  8.     .Pattern = "(\s+)(\b\d\S*?\b\s+?\S)"
  9. End With
  10. Set Arr = RegEx.Execute(cc)
  11. tt = RegEx.Replace(cc, "$1" & Chr(13) & "$2")
  12. Set RegEx = Nothing
  13. xyh9999k = tt
  14. End Function


运行结果:

  1. Four Cheese Ravioli
  2. Ingredients
  3. Ravioli Dough:
  4. 2 cups all-purpose flour
  5. 1 pinch salt
  6. 1 teaspoon olive oil
  7. 2 eggs
  8. 1 1/2 tablespoons water
  9. Ravioli Filling:
  10. 1 (8 ounce) container ricotta cheese
  11. 1 (4 ounce) package cream cheese, softened
  12. 1/2 cup shredded mozzarella cheese
  13. 1/2 cup provolone cheese, shredded
  14. 1 egg
  15. 1 1/2 teaspoons dried parsley
  16. Pesto-Alfredo Cream Sauce:
  17. 2 tablespoons olive oil
  18. 2 cloves garlic, crushed
  19. 3 tablespoons prepared basil pesto
  20. sauce
  21. 2 cups heavy cream
  22. 1/4 cup grated Parmesan cheese
  23. 1 (24 ounce) jar marinara sauce
  24. Egg Wash:
  25. 1 egg
  26. 1 tablespoon water
4楼
xyh9999
请k哥再发些练习,正好这几天复习过,做做测试以便巩固一下。

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap