Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to select everything between two characters?

Tags:

regex

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!

like image 593
Jaaq Avatar asked Jul 26 '10 14:07

Jaaq


People also ask

How do you use wildcards in regex?

In regular expressions, the period ( . , also called "dot") is the wildcard pattern which matches any single character. Combined with the asterisk operator . * it will match any number of any characters.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.

What is a capturing group regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is \r and \n in regex?

\n. Matches a newline character. \r. Matches a carriage return character.


1 Answers

How about

"§([^;]*);" 

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

like image 82
mdma Avatar answered Oct 06 '22 01:10

mdma