Regular key capture issue

I have this template template:

Hello {#Name #},

Thank you for coming on {# Date #} and we are glad to see you here again {#President #}

So, I'm trying to get parts of the patterns {#...#} and put them in an array.

But my expression did not work:

 \b(?<=\{\#)(.*)(?=\#\})\b 

The result is something like this:

 {#Something#} Hello {#Brand#} 

Result:

Something#} Hello {#Brand

+4
source share
2 answers

Just add? for laziness like this:

 \b(?<=\{\#)(.*?)(?=\#\})\b 

*? means that he will look for as few repetitions as possible

+4
source

How about this? {#([^#]+)#}

Here is an example used in a PowerShell script:

 $input = "{#Something#} Hello {#Brand#}" $match = [regex]::Match($input, "{#([^#]+)#}") $i = 0 while ($match.Success) { $i++ write-host ("Match {0}: '{1}'" -f $i, $match.Groups[1].Value) $match = $match.NextMatch() } 

And here is what he infers:

 Match 1: 'Something' Match 2: 'Brand' 
+3
source

Source: https://habr.com/ru/post/1307378/


All Articles