Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp - How to find text parts surrounded by two specific characters?

Tags:

regex

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?

like image 975
BadmintonCat Avatar asked May 19 '11 12:05

BadmintonCat


1 Answers

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 +.

like image 78
Kaos Avatar answered Nov 11 '22 19:11

Kaos