Regex Question to Grab Keys
I have this kinda template text :
Hello {#Name#},
Thanks for coming blah on {#Date#} and we love to see you again here with {#President#}
So I am tryin开发者_运维问答g to get {#...#}
templates parts and put them into an array.
But my expression didn't work :
\b(?<=\{\#)(.*)(?=\#\})\b
The result became something like this for this sample text :
{#Something#} Hello {#Brand#}
Result :
Something#} Hello {#Brand
Just add ? for laziness like this:
\b(?<=\{\#)(.*?)(?=\#\})\b
*? means that it will search for as few repeats as possible
How about this? {#([^#]+)#}
Here's the 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 this is what it outputs:
Match 1: 'Something'
Match 2: 'Brand'
精彩评论