it’s been a while since the last time I had to use regex, I am kind of in a hurry to accomplish something so I hope I can get a quick answer to this quick question.
Say I have the following text:
Start
A
B
C
End
Start
A
B
C
End Start
A
B
C
End
Foo
A
B
C
Bar
I would like to replace the line breaks with pipes but only between the “Start” and “End” words so that my end result is:
Start|A|B|C|End
Start|A|B|C|End Start|A|B|C|End
Foo
A
B
C
Bar
Thank you very much.
When you start parsing expressions like that, you’re not in regex territory anymore. Similar to XML, expressions where you need to treat the same character differently based on its context is a class of language higher than regular expressions.
A more traditional approach of just poking through the string directly would work better in this situation.
Assuming the original string is split up by whitespace as your example showed, you can just split the string on any whitespace, and set a flag when you are between a
StartandEndtoken to put pipes between tokens instead of newlines.