Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx match text in between delimiters

Tags:

regex

I need a regex that extract text inside a delimiter but I'm having problem extracting the value inside the delimiter [DATA n] and [END DATA]

Here's my regex

(?<=\[DATA\s+\d+\]).*(?=\[END DATA\])

Here's example data I want to match

Some text here

[DATA 1]
data one 
some more data
[END DATA]
[DATA 2]
data two
more data
data
[END DATA]
[DATA n]
more data 
data 
[END DATA]
like image 880
dynamicvoid Avatar asked Sep 13 '10 03:09

dynamicvoid


People also ask

How do I match a specific character in regex?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.

What are delimiters in regex?

Delimiters. The first element of a regular expression is the delimiters. These are the boundaries of your regular expressions. The most common delimiter that you'll see with regular expressions is the slash ( / ) or forward slash.


1 Answers

You appear to be using regular expressions features like lookbehind and lookahead when you don't really need them. Try:

\[DATA\s+\d+\](.*?)\[END DATA\]

There's only one capture group in this regular expression, (.*?). After using this, the result you're looking for should be in capture group 1.

Note also that I've used the non-greedy .*? match that will match up until the first following instance of [END DATA]. Without this, if you use just .*, you'll capture everything up to the last [END DATA].

like image 103
Greg Hewgill Avatar answered Sep 28 '22 07:09

Greg Hewgill