【发布时间】:2009-03-03 21:33:59
【问题描述】:
好的,我正在尝试清理一个多行字符串。
每一行可能是也可能不是大块引用文本的一部分。示例:
This line is not quoted.
This part of the line is not quoted “but this is.”
This one is not quoted either.
“This entire line is quoted”
Not quoted.
“This line is quoted
and so is this one
and so is this one.”
This is not quoted “but this is
and so is this.”
我需要一个正则表达式替换来解开硬包装的引号行,即用空格替换 "\r\n",但只能在大引号之间。
更换后的外观如下:
This line is not quoted.
This part of the line is not quoted “but this is.”
This one is not quoted either.
“This entire line is quoted”
Not quoted.
“This line is quoted and so is this one and so is this one.”
This is not quoted “but this is and so is this.”
(注意最后两行是输入文本中的多行。)
约束
- 理想情况下需要一个正则表达式替换调用
- 使用 .NET RegEx 库
- 引号总是开始/结束大引号,而不是普通的双引号 ("),这应该会更容易一些。
重要约束
这不是直接的 .NET 代码,我正在填充一个“searchfor/replacewith”字符串表,然后通过 RegEx.Replace 调用这些字符串。我无法添加自定义代码,如匹配评估器、循环捕获的组等。
到目前为止的当前答案,大致如下:
r.Replace("(?<=“)\r\n(?=”)", " ")
显然,我还没有接近。
同样的逻辑可以应用于编程代码中块 cmets 的颜色编码——块注释内的任何内容都不会与 cmets 之外的内容相同。 (代码有点棘手,因为开始/结束块注释分隔符也可以合法地存在于文字字符串中,我不必在这里处理这个问题。)
【问题讨论】:
-
您能否提供限制的任何原因,尤其是前两个?
-
添加...这不是自定义代码,它是我编写的通用解析/清理工具,它是从数据库表驱动的。没有一种简单的方法可以为这个特定问题编写一次性代码。