tags:

views:

68

answers:

2

Hello,

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 trying 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

+4  A: 

Just add ? for laziness like this:

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

*? means that it will search for as few repeats as possible

Hun1Ahpu
It didn't work either. The result is empty.
Braveyard
@Braveyard - Sorry. One unnecessary bracket. Check again
Hun1Ahpu
+3  A: 

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'
Damian Powell
It didn't work. `{{` is the result. I am using Expresso to evaluate the results.
Braveyard
I used RegexBuddy - works on my machine! I'll update my answer with a piece of PowerShell to prove the point.
Damian Powell
I think it should be difference between Regex engines? Because `{` should be written with `\\` to indicate that is a literal rather than a symbol.
Braveyard
No, I don't think so because PowerShell uses the exact same engine that C# uses because it is using the .NET Framework. [regex]::Match(...) in PowerShell is *exactly* the same as Regex.Match(...) in C#. It only needs to be escaped if it contains numbers because in that case it is a repetition clause.
Damian Powell