how.wtf

Match everything between two characters in Regex

· Thomas Taylor

Matching everything between two delimiters in Regex is supported. In the examples below, the [ and ] characters were used as example delimiters.

Match between characters using lookahead / lookbehind

Using lookbehind and lookahead, the following patterns will match between two delimiters:

1(?<=\[)(.*?)(?=\])

Using regex101, the explanation can summarized as:

Match the non-greedy capture group (.*?) that is preceded by a [ and is followed by ] but do not capture the delimiters.

Match between characters without lookahead / lookbehind

Without using lookbehind or lookahead, the following pattern will match contents between two delimiters including the delimiters:

1\[(.*?)\]

Here is the regex101 link.

To remove the delimiters, the first captured group of each match will need to be used.

#Regex  

Reply to this post by email ↪