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'
/{(.+?)}/
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.
{(?<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.
>>> import re
>>> s = 'The Insured Addr {Address} is not valid for {Name}'
>>> re.findall('\{([a-zA-Z0-9 ]+)\}', s)
['Address', 'Name']
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 .*?).
/\{([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']
\{(.*?)\}
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".
{([^}]*)} Will grab the first one.
加载中,请稍侯......
精彩评论