-2

如何使用正则表达式删除下面的文本块的所有\r\n字符(将其转换为仅 1 行) ?<out>...</out>

<nx1>home</nx1>
<nx2>living</nx2>
<out>text one
text continues
and at last!</out>
<m2>dog</m2>

此文本示例的最终结果应为:

<nx1>home</nx1>
<nx2>living</nx2>
<out>text one text continues and at last!</out>
<m2>dog</m2>
4

1 回答 1

4

搜索

(?<=<out>(?:(?!</?out>).)*)\r\n(?=(?:(?!</?out>).)*</out>)

并用一个空格替换所有内容。在 EditPad Pro 7.3.1 上测试。确保选择“点”选项,因此点也匹配换行符。

解释:

(?<=               # Look behind to assert that there is...
 <out>             # an <out> tag before the current position,
 (?:(?!</?out>).)* # followed by anything except <out> or </out> tags
)                  # End of lookbehind
\r\n               # Match \r\n
(?=                # Look ahead to assert that there is...
 (?:(?!</?out>).)* # any number of characters ahead (except <out>/</out> tags)
 </out>            # followed by an </out> tag
)                  # End of lookahead
于 2014-05-27T11:06:33.330 回答