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]
You appear to be using regular expressions features like lookbehind and lookahead when you don’t really need them. Try:
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].