How to delete “\\r\\n”, but only in certain sectio

2019-09-22 10:22发布

问题:

How can I delete all \r\n characters (to convert it to just 1 line) just for the <out>...</out> block of the text below using regex?

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

The final result for this sample of text should be:

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

回答1:

Search for

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

and replace all with a single space. Tested on EditPad Pro 7.3.1. Make sure you select the "Dot" option, so the dot also matches newlines.

Explanation:

(?<=               # 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


标签: regex editpad