Re: Regular Expression Hangs



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.

.



Relevant Pages

  • Re: Regular Expression Hangs
    ... You wouldn't need a regular expression to ... a single fixed string. ... Those patterns are defined by rules that are expressed in the ... will capture the following: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Looking for help with Regular Expression
    ... capture a string of text that falls between an opening squre bracket ... The end of my string sometimes contains a cross ... But my regular expression is stopping after the first "]" so after I ... I'm not sure how to make my capture more greedy so I've resorted to ...
    (comp.lang.python)
  • Re: Which RegEx Testing Tool Do You Prefer?
    ... public static string ReplaceAmpersand ... I used both Expresso and Regex Buddy to come up with this. ... > have to force the 2.0 Regular Expression Validator to fail when the & is ... >> create patterns of a large variety of types. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Looking for help with Regular Expression
    ... capture a string of text that falls between an opening squre bracket ... The end of my string sometimes contains a cross ... But my regular expression is stopping after the first "]" so after I ... I'm not sure how to make my capture more greedy so I've resorted to ...
    (comp.lang.python)
  • Re: Which RegEx Testing Tool Do You Prefer?
    ... force the 2.0 Regular Expression Validator to fail when the & is present in ... >> The kind of RegEx tool I'd like is one which can take a string ... > matches a literal string quite easily. ... > create patterns of a large variety of types. ...
    (microsoft.public.dotnet.framework.aspnet)

Loading