tags:

views:

61

answers:

7

Hi All,

I want to find all the words enclosed within a {} using a regular expression.

If my string is 'The Insured Addr {Address} is not valid for {Name}',

I want to pull out only 'Address' and 'Name' . I have been trying for couple of hours without success.

Any help is really appreciated.

I tried ([/{]([a-zA-Z0-9 ]*)[}])

This does not work.

I am using C#. Also the templates can contain dot notated properties as in 'Address.city','ABC.PQR.XYZ'

+1  A: 
/\{([a-z0-9]+)\}/i

I don't know what language you are using, but the word inside braces will be captured into the first submatch group.

For instance, using Ruby:

str = 'The Insured Addr {Address} is not valid for {Name}'
matches = str.scan(/\{([a-z0-9]+)\}/i).flatten
# => ['Address', 'Name']
Daniel Vandersluis
A: 

\{(.*?)\}

The *? instead of * is for lazyness, to not catch {Address} is not valid for {Name} as one long unit. See here under "Laziness Instead of Greediness".

Y. Shoham
Since `[a-zA-Z0-9 ]` does not match `}`, there is no need for laziness.
Bart Kiers
Oops. :S Good point. I edited my answer.
Y. Shoham
+1  A: 
{(?<name>\w+)}

This will capture the text within the { and } within a group named name.

In C#:

Regex r = new Regex(@"{(?<name>.+?)}");
MatchCollection coll = r.Matches("The Insured Addr {Address} is not valid for {Name}");
foreach (Match m in coll) {
    Console.WriteLine(m.Groups["name"]);
}

Prints

Address
Name

on the console.

Jason
that solves the issue. It will not work if I have Name.FirstName or something like Address.City.Zip
Yogendra
Addressed; see my edit.
Jason
+1  A: 
>>> import re
>>> s = 'The Insured Addr {Address} is not valid for {Name}'
>>> re.findall('\{([a-zA-Z0-9 ]+)\}', s)
['Address', 'Name']
SilentGhost
Probably a typo: `[a-zA-z]` should probably be `[a-zA-Z]`.
Bart Kiers
@Bart: thanks, well caught :)
SilentGhost
A classic regex-typo! :)
Bart Kiers
+1  A: 

The regex:

\{([^}]*)}

will match both '{Address}' and '{Name}' and will capture 'Address' and 'Name' in match group 1. Because [^}] also matches line breaks, it will also work if { and } are on a different line (which is not the case with .*?).

Bart Kiers
+2  A: 
/{(.+?)}/

This will get anything inside of braces, even multiple words.

If you expect that you might have whitespace padding, you could use:

/{\s*([\S+]+?)\s*}/

This will mean that {Address} and { Address } return the same thing. In this version, no other spaces are allowed in the tag, but you could just as easily do (.+?). The ? means that it will find a word within the two closest braces.

Jeff B
A: 

{([^}]*)} Will grab the first one.

MikeL