如何删除“\ r \ n”,但仅限于我的文本的某些部分?

时间:2014-05-27 10:58:17

标签: regex editpad

如何使用正则表达式删除所有\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>

1 个答案:

答案 0 :(得分: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