Re: Regular Expression Hangs
- From: shawnmkramer@xxxxxxxxxxx
- Date: 18 May 2007 12:13:57 -0700
On May 18, 12:50 pm, "Kevin Spencer" <unclechut...@xxxxxxxxxxxx>
wrote:
Well, your regular expression is a mess for starters. I would suggest an
alternative, but you haven't given us any rules regarding the pattern(s)
you're trying to match. What you said was:
trying to match the following data:
ENVIRONMENTAL RESTORATION AND WASTE MANAGEMENT, DOE (US)
That is obviously not true. You wouldn't need a regular expression to match
a single fixed string. A regular expression searches for patterns in
strings. Those patterns are defined by rules that are expressed in the
regular expression. And the regular expression you posted, besides being a
mess (I will get to that), couldn't possibly match that string, since it
contains the literal ", City of, " - which is nowhere to be found in your
posted string.
The reason it's a mess is that you have many more Groups than you probably
know. You have 3 named Groups ("OrgCity," "OrgState," and "OrgCountry"), but
you also have FIVE unnamed Groups, and you're using backreferencing, so I'm
not sure where the compiler is throwing up on you.
Because I don't know the rules, I can't really give you a full answer.
However, I can tell you this much:
"^(?<OrgCity>[\w\s]+),"
will capture the following:
ENVIRONMENTAL RESTORATION AND WASTE MANAGEMENT,
Everything but the comma will be in the Group "OrgCity"
"\((?<OrgCountry>[\w\s]{2,})\)?$"
will capture the following:
(US)
Everything but the parentheses will be in the group "OrgCountry"
As for your third Group, I simplified the regular expression to the
following, which has the same rules:
(?<OrgState>[A-Z]{2}|[A-Z][a-z]+\.)
Briefly, it captures 1 of 2 possible patterns:
2 Capital letters
-or-
1 Capital letter followed by 1 or more lower-case letters, followed by a
period
That's the best I can do!
--
HTH,
Kevin Spencer
Microsoft MVP
Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net
<shawnmkra...@xxxxxxxxxxx> wrote in message
news:1179499497.473590.212160@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Anyone every heard of the Regex.IsMatch and Regex.Match methods just
hanging and eventually getting a message "Requested Service not
found"?
I have the following pattern:
^(?<OrgCity>([A-Z][\w ]+)+), City of, (?<OrgState>(([A-Z][A-Z])|([A-Z]
[a-z]+\.)))( \((?<OrgCountry>[\w ]{2,})\))?$
(ignore the line wrap)
trying to match the following data:
ENVIRONMENTAL RESTORATION AND WASTE MANAGEMENT, DOE (US)
The following code will simply hang for a long time then write the
message "Requested Service not found" to the debug console:
Regex myRegex = new Regex(@"^(?<OrgCity>([A-Z][\w ]+)+), City of, (?
<OrgState>(([A-Z][A-Z])|([A-Z][a-z]+\.)))( \((?<OrgCountry>[\w ]{2,})
\))?$", RegexOptions.Compiled);
myRegex.IsMatch("ENVIRONMENTAL RESTORATION AND WASTE MANAGEMENT, DOE
(US)");- Hide quoted text -
- Show quoted text -
I think you missed the point. My post was not for help on how to match
some pattern. It's about why the regex library has this unpredictable
behavior.
I actually intended for the pattern to NOT match that string. I see
your point about having unneccessary capturing groups though, but
there not a problem for what I'm trying to capture.
.
- Follow-Ups:
- Re: Regular Expression Hangs
- From: Kevin Spencer
- Re: Regular Expression Hangs
- References:
- Regular Expression Hangs
- From: shawnmkramer
- Re: Regular Expression Hangs
- From: Kevin Spencer
- Regular Expression Hangs
- Prev by Date: Re: Application.Run() In a Windows Service
- Next by Date: Re: Is Ctrl key pressed?
- Previous by thread: Re: Regular Expression Hangs
- Next by thread: Re: Regular Expression Hangs
- Index(es):
Relevant Pages
|
Loading