-2

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>
wiki
  • 1,877
  • 2
  • 31
  • 47
  • I'm using EditPad Pro regex engine; I tried to use lookarounds but without success; sth like: (?<!)\r\n – wiki May 27 '14 at 11:03

1 Answers1

4

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
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • thanks a lot man; it worked just fine; Can you describe the pattern? – wiki May 27 '14 at 11:09
  • EditPad pro team can make an amazing software by adding the possibility of using secondary, tertiary, ...patterns; I mean with a simple pattern like `\Q\E.+?\Q\E` we highlight the excerpt text for continuing the search and after that, within the highlighted text just a simple find of `\r\n` and replace it with a space! – wiki May 27 '14 at 11:44
  • Upvoting for the explanation ... I hate regex – Ahmad Alfy May 27 '14 at 11:48