如何在VBAmacros中用正则expression式parsingExcel的单元格string

我在Excel 2010中编写一个macros,以删除列的多个单元格中的换行符。 实际上,我有5个案子,我必须在单元格内search。 我列举如下:

  1. 如果有一个点或一个逗号后跟一个空格,然后是一个换行符(\ n),则用点或逗号(不pipe它在应用正则expression式之前)replace它,后跟一个空格
  2. 如果有一个字符,然后是空格,然后是换行符,请将其replace为字符和空格
  3. 如果有一个字符后跟换行符,请用字符+空格replace
  4. 如果有一个字符后跟两个空格,然后是换行符,则用字符+空格replace它
  5. 如果在行末有一个点,那就把它留下来,因为这是一个终点。

正如你所看到的,2和3是非常相似的,所以我认为正则expression式可能是类似于[a-zA-Z0-9]\n但我不知道…首先,如果search换行符只是加\ n和秒,如何search一个空格。 检测到正则expression式后,我认为可以用一个.Replace(Text,"regexp","regexp ")来解决, .Replace(Text,"regexp","regexp ")空格是从“char”+“”
所以基本上我的问题是,这种模式的正则expression式是什么? 在第五种情况下,我怎样才能search行终止符,所以它不会尝试在一个段的最后一个点后面search换行符。
我可以使用Chr(10)换行,Chr(32)使用空格?
顺便说一句,我一直在关注这些引用:
如何使用RegExp
VBA拆分string

这种模式会find任何字母数字字符. ,或者,然后是可选的空格,然后是换行符,并将其replace为匹配的结束字符,后跟一个空格。

 Dim re Set re = CreateObject("VBScript.RegExp") re.Pattern = "([\w.,])\s*\n" strText = re.Replace(strText, "$1 ") 

testing输出。 忽略括号。 他们只是在那里显示空间。

 [the end.] = [the end.] [the end.\n] = [the end. ] [the end. \n] = [the end. ] [the end. \n] = [the end. ] [the end,] = [the end,] [the end,\n] = [the end, ] [the end, \n] = [the end, ] [the end, \n] = [the end, ] [the end] = [the end] [the end\n] = [the end ] [the end \n] = [the end ] [the end \n] = [the end ]