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]
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.
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.
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With