|
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)");
or
Match 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[^].
|
|
|
|
|
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.
|
|
|
|
|
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?
|
|
|
|
|
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.
|
|
|
|
|
Hi, I would like to search xml file using lucene dll. I'm finding difficulty in creating index files.
Please guide me. Thanks in advance
modified on Friday, May 13, 2011 3:59 AM
|
|
|
|
|
When you have a question on a project like this, it's normally better to ask this question on the forums for that project as they are the experts in it. Have a search through the support forums there[^].
|
|
|
|
|
Hi,
I just asked myself what might be the future of C#?
Is the language going to be around for the next 5 years or maybe more?
Hope someone has an answer
|
|
|
|
|
Well, it's been around for the last 10 years, so yeah, I think it'll be around for lots longer.
|
|
|
|
|
yes.
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.
|
|
|
|
|
Keep in mind that C# is promoted by Microsoft. Microsoft, whatever its critics may say, is a company with quite a high degree of perseverance, persistence, resilience. Microsoft will make sure that C# continuously grows and meets the ever-increasing demands of the developer community.
|
|
|
|
|
In addition, through Mono, C#/.Net has the power to stick around even without Microsoft. The language is also full of nice features and in 4.0 (with all the parallel stuff) the framework is set up well for the multi-core world (the next 10 years?).
|
|
|
|
|
I wouldn't bank on Mono. There's some doubt over the future of it - pretty much the entire Mono team was sacked by their new owners.
|
|
|
|
|
I heard a rumor, though, that the main architect of C# is moving to a smaller company to develop a Pascal-based programming environment.
|
|
|
|
|
Yea me too but he said he isn't leaving, he will work on both.
|
|
|
|
|
Yeah, right. "No, really, Honey. We're just good friends."
|
|
|
|
|
Languages don't just die; you can use it as long as you like even if you're the only one.
|
|
|
|
|
So you're the one MUMPS-11 user remaining?
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.
|
|
|
|
|
No, never learned that -- but I hear it lives on in Caché.
Also, I've forgotten all the Pascal, Lisp, COBOL, and Fortran I learned in college.
|
|
|
|
|
You actually learned Fortran I? (I'd guess it was simply called Fortran at the time). I started out with Fortran II, immediately moved on to Fortran IV and Watfor, soon to be followed by Watfiv.
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.
|
|
|
|
|
Fortran 78? In 1986 on a VAX 11-780. Good times.
|
|
|
|
|
I was punching cards and feeding them to IBM machinery in the early seventies...
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.
|
|
|
|
|
But were they good times?
|
|
|
|