Problem matching name using Regex
-
Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using:
\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b
..and here is the string I`m matching:"JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;"
Like I said, in Expresso it matches the full name, but when I use:MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
orMatch m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
it doesn't return any matches. Anything I could be doing wrong? -
Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using:
\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b
..and here is the string I`m matching:"JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;"
Like I said, in Expresso it matches the full name, but when I use:MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
orMatch m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
it doesn't return any matches. Anything I could be doing wrong?Are you missing parentheses around your character class for upper and lower-case letters? I think [a-z][A-Z]* in both places should be replaced with ([a-z][A-Z])+. P.S. There is a separate forum for regular expressions[^].
-
Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using:
\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b
..and here is the string I`m matching:"JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;"
Like I said, in Expresso it matches the full name, but when I use:MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
orMatch m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
it doesn't return any matches. Anything I could be doing wrong?Firstly, we do have a Regex forum: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] - it might be more appropriate in future. Secondly, in Expresso, go to the Design tab and look at the bottom: You will find you have "Ignore case" selected. If you de-select that, Expresso will not find it either. Change your string to:
\b[a-zA-Z][a-zA-Z]*\b\s*\b[a-zA-Z][a-zA-Z]*\b
And it will work in both, or use the RegexOption.IgnoreCase as part of the Match method. MSDN[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Firstly, we do have a Regex forum: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] - it might be more appropriate in future. Secondly, in Expresso, go to the Design tab and look at the bottom: You will find you have "Ignore case" selected. If you de-select that, Expresso will not find it either. Change your string to:
\b[a-zA-Z][a-zA-Z]*\b\s*\b[a-zA-Z][a-zA-Z]*\b
And it will work in both, or use the RegexOption.IgnoreCase as part of the Match method. MSDN[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
Thanks I only noticed that now.
-
Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using:
\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b
..and here is the string I`m matching:"JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;"
Like I said, in Expresso it matches the full name, but when I use:MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
orMatch m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
it doesn't return any matches. Anything I could be doing wrong?Griff's answer seems appropriate for the question you asked, however your question may be a bit shortsighted, as names can be a bit more complex than you are expecting. Here are a few CP member names you would still have trouble with:
wout de zeeuw
Pete O'Hanlon
Smithers-Jones
Ennis Ray Lynch, Jr.What I would do is locate the first digit or @ sign, then get everything that sits before the last space in front of that. And no, I would not use regex to implement that, it is way simpler using two methods from the string class. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Griff's answer seems appropriate for the question you asked, however your question may be a bit shortsighted, as names can be a bit more complex than you are expecting. Here are a few CP member names you would still have trouble with:
wout de zeeuw
Pete O'Hanlon
Smithers-Jones
Ennis Ray Lynch, Jr.What I would do is locate the first digit or @ sign, then get everything that sits before the last space in front of that. And no, I would not use regex to implement that, it is way simpler using two methods from the string class. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
What I would do is locate the first digit ...
Perhaps you aren't familiar with Tom Lehrer's friend Hen3ry? :-D
-
Luc Pattyn wrote:
What I would do is locate the first digit ...
Perhaps you aren't familiar with Tom Lehrer's friend Hen3ry? :-D
Indeed I'm not. Plz send appropriate regex codez. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.