tags:

views:

68

answers:

3

Hi there, I am trying to write a regex that selects everything between two characters.

For example, when the regex encounters a '§' I want it to select everything after the '§' sign, up until the point that the regex encounters a ';'. I tried with a lookbehind and lookahead, but they don't really do the trick.

So for example " § 1-2 bla; " should return " 1-2 bla".

Any help would be greatly appreciated!

A: 

For a simple case this should do:

§(.*);

It might need to be modified if you don't want to allow nesting:

§(.*?);
SilentGhost
This isn't quite correct - the `.*` is greedy, so the expression will match upto the *last* `;` encountered, not the first one.
Peter Boughton
@Peter: it might not be the issue for OP, and it might be what OP actually wants.
SilentGhost
Well based on "up until the point that the regex encounters a ';'" -- I take that to mean it *shouldn't* be greedy.
Peter Boughton
@Peter: well based on given example it doesn't matter if quantifier is greedy or not.
SilentGhost
+3  A: 

Use this regex

(?<=§).*?(?=;)
Gopi
I'd go with the clearer `[^;]*` over `.*?` but still should be ok. Still, +1 for using lookarounds. :)
Peter Boughton
:) good suggestion. thanks.
Gopi
+6  A: 

How about

"§([^;]*);"

The selected characters between the § and ; are available as match group 1.

mdma