Search code examples
phpregexpreg-match

Regex return string between two matches


I'm trying to get the string between two specific matches, for example two hashtags.

What I would like to achieve:

input: some text in front ##hi there## some text behind

output: hi there

With /%%(.*)%%/, ##hi there## is returned.

Thanks in advance


Solution

  • You need a look ahead and look behind.

    (?<=##).*(?=##)
    

    This will match hi there

    https://regex101.com/r/qUR4NR/1