I'm trying to figure out a regexp that replaces text parts that are surrounded by a specific character... take a look at this example:
\|(.+)\|
it finds any text that is placed between two vertical lines (|) and if there's only one occurrence of this in the text it works find but if there are two or more it'll find all text that is between the first found | and the last found | ...
Lorem ipsum dolor sit amet, |consectetur| adipiscing elit. Aliquam nec |tortor lectus|. Nunc sodales ornare varius. Quisque pharetra porttitor ligula.
... however in the example text I would only want to have 'consectetur' and 'tortor lectus' found. How do I need to improve the regexp to exclude all the text inbetween?
The + is greedy. Try *, which should match as little as possible (non-greedy). Edit: I was mixed up in my head. Add a ? to make it non-greedy:
\|(.*?)\|
The same goes for +:
\|(.+?)\|
If you need the 1 or more of +, add another dot. (edit: which may not be a good idea if you have two | next after each other).
Edit 2: added the ? to make it non-greedy
\|(..*?)\|
.
The accepted suggestion
There is perhaps an even better approach, to not match any character, but to match all but |, as in:
\|([^|]+)\|
Then you can still have the greedy +.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With