Connecting Tech Pros Worldwide Forums | Help | Site Map

RegEx Not Working In .NET

BigAl
Guest
 
Posts: n/a
#1: Nov 16 '05
Perhaps someone here can help me out...

RegEx: "^.*\d{5}(-\d{4})?.*$"
Intended Purpose: To strip out the City/State/ZipCode line from a signature.
Sample Text:

Joe Jackson
131 W. 5th Street
New York, NY 10023

RegEx Should Return: "New York, NY 10023"

1) This RegEx works correctly in Excel using "Microsoft VBScript Regular
Expressions 5.5" object library
2) This RegEx works correctly with web-based .NET processor on
http://www.regexlib.com/RETester.aspx
3) This RegEx DOES NOT WORK in .NET v1.1 (well, at least not for me!)
4) I found the article "FIX: The Regex class and the Match class may not
correctly find matches for a regular expression" on Microsoft Support site
(http://support.microsoft.com/default...;en-us;822923), however the
versions of the files that they say create the fix are OLDER than the ones I
have, so perhaps this is a fix for .NET v1.0. ???

Robby
Guest
 
Posts: n/a
#2: Nov 16 '05

re: RegEx Not Working In .NET



One way to solve this is to set the multiline option,
RegexOptions.Multiline.

Dim regexCityLine As New Regex( _
"^.*\d{5}(-\d{4})?.*$", _
RegexOptions.Multiline)

Robby
VB.Net


"BigAl" <BigAl@discussions.microsoft.com> wrote in message
news:EE20FFCD-EE56-47DA-9CA9-0CBED9F5640A@microsoft.com...[color=blue]
> Perhaps someone here can help me out...
>
> RegEx: "^.*\d{5}(-\d{4})?.*$"
> Intended Purpose: To strip out the City/State/ZipCode line from a
> signature.
> Sample Text:
>
> Joe Jackson
> 131 W. 5th Street
> New York, NY 10023
>
> RegEx Should Return: "New York, NY 10023"
>
> 1) This RegEx works correctly in Excel using "Microsoft VBScript Regular
> Expressions 5.5" object library
> 2) This RegEx works correctly with web-based .NET processor on
> http://www.regexlib.com/RETester.aspx
> 3) This RegEx DOES NOT WORK in .NET v1.1 (well, at least not for me!)
> 4) I found the article "FIX: The Regex class and the Match class may not
> correctly find matches for a regular expression" on Microsoft Support site
> (http://support.microsoft.com/default...;en-us;822923), however
> the
> versions of the files that they say create the fix are OLDER than the ones
> I
> have, so perhaps this is a fix for .NET v1.0. ???[/color]


BigAl
Guest
 
Posts: n/a
#3: Nov 16 '05

re: RegEx Not Working In .NET


I've tried both SingleLine and MultiLine options, and neither seem to work.

"Robby" wrote:
[color=blue]
>
> One way to solve this is to set the multiline option,
> RegexOptions.Multiline.
>
> Dim regexCityLine As New Regex( _
> "^.*\d{5}(-\d{4})?.*$", _
> RegexOptions.Multiline)
>
> Robby
> VB.Net
>
>
> "BigAl" <BigAl@discussions.microsoft.com> wrote in message
> news:EE20FFCD-EE56-47DA-9CA9-0CBED9F5640A@microsoft.com...[color=green]
> > Perhaps someone here can help me out...
> >
> > RegEx: "^.*\d{5}(-\d{4})?.*$"
> > Intended Purpose: To strip out the City/State/ZipCode line from a
> > signature.
> > Sample Text:
> >
> > Joe Jackson
> > 131 W. 5th Street
> > New York, NY 10023
> >
> > RegEx Should Return: "New York, NY 10023"
> >
> > 1) This RegEx works correctly in Excel using "Microsoft VBScript Regular
> > Expressions 5.5" object library
> > 2) This RegEx works correctly with web-based .NET processor on
> > http://www.regexlib.com/RETester.aspx
> > 3) This RegEx DOES NOT WORK in .NET v1.1 (well, at least not for me!)
> > 4) I found the article "FIX: The Regex class and the Match class may not
> > correctly find matches for a regular expression" on Microsoft Support site
> > (http://support.microsoft.com/default...;en-us;822923), however
> > the
> > versions of the files that they say create the fix are OLDER than the ones
> > I
> > have, so perhaps this is a fix for .NET v1.0. ???[/color]
>
>
>[/color]
Doug Holton
Guest
 
Posts: n/a
#4: Nov 16 '05

re: RegEx Not Working In .NET


BigAl wrote:[color=blue]
> Perhaps someone here can help me out...
>
> RegEx: "^.*\d{5}(-\d{4})?.*$"
> Intended Purpose: To strip out the City/State/ZipCode line from a signature.
> Sample Text:
>
> Joe Jackson
> 131 W. 5th Street
> New York, NY 10023
>
> RegEx Should Return: "New York, NY 10023"[/color]

You should post what code you were using.
Assuming you were passing all 3 lines (instead of splitting them up and
passing the last line only), you can try the regex the code below. The
code is in the language boo: http://boo.codehaus.org/

s = """
Joe Jackson
131 W. 5th Street
New York, NY 10023
"""

r =
/(?<=\n)\s*(?<city>[^\n]+)\s*,\s*(?<state>\w+)\s+(?<zip>\d{5}(-\d{4})?).*$/.Match(s)

print r.Groups["city"]
print r.Groups["state"]
print r.Groups["zip"]
Robby
Guest
 
Posts: n/a
#5: Nov 16 '05

re: RegEx Not Working In .NET



It works for me in VB.Net and C# uses the same Regex object. Can you post
the code you are using so I can see why it is not working for you?

Robby

"BigAl" <BigAl@discussions.microsoft.com> wrote in message
news:7739F1F9-3985-4555-9544-55075AFBA142@microsoft.com...[color=blue]
> I've tried both SingleLine and MultiLine options, and neither seem to
> work.
>
> "Robby" wrote:
>[color=green]
>>
>> One way to solve this is to set the multiline option,
>> RegexOptions.Multiline.
>>
>> Dim regexCityLine As New Regex( _
>> "^.*\d{5}(-\d{4})?.*$", _
>> RegexOptions.Multiline)
>>
>> Robby
>> VB.Net
>>
>>
>> "BigAl" <BigAl@discussions.microsoft.com> wrote in message
>> news:EE20FFCD-EE56-47DA-9CA9-0CBED9F5640A@microsoft.com...[color=darkred]
>> > Perhaps someone here can help me out...
>> >
>> > RegEx: "^.*\d{5}(-\d{4})?.*$"
>> > Intended Purpose: To strip out the City/State/ZipCode line from a
>> > signature.
>> > Sample Text:
>> >
>> > Joe Jackson
>> > 131 W. 5th Street
>> > New York, NY 10023
>> >
>> > RegEx Should Return: "New York, NY 10023"
>> >
>> > 1) This RegEx works correctly in Excel using "Microsoft VBScript
>> > Regular
>> > Expressions 5.5" object library
>> > 2) This RegEx works correctly with web-based .NET processor on
>> > http://www.regexlib.com/RETester.aspx
>> > 3) This RegEx DOES NOT WORK in .NET v1.1 (well, at least not for me!)
>> > 4) I found the article "FIX: The Regex class and the Match class may
>> > not
>> > correctly find matches for a regular expression" on Microsoft Support
>> > site
>> > (http://support.microsoft.com/default...;en-us;822923),
>> > however
>> > the
>> > versions of the files that they say create the fix are OLDER than the
>> > ones
>> > I
>> > have, so perhaps this is a fix for .NET v1.0. ???[/color]
>>
>>
>>[/color][/color]


Hollenho
Guest
 
Posts: n/a
#6: Nov 16 '05

re: RegEx Not Working In .NET


BigAl,

Your Regex works fine for me in Expresso using Framework 1.1. Perhaps there
is a problem in your code. You should set Multiline ON and Singleline OFF.

Here is the C# Regex definition as generated by Expresso:

public static Regex regex = new Regex(
@"^.*\d{5}(-\d{4})?.*$",
RegexOptions.Multiline
| RegexOptions.Compiled
);

Expresso can be used to debug your regular expression, is free, and can be
found at http://www.ultrapico.com

Regards,

Jim

"BigAl" <BigAl@discussions.microsoft.com> wrote in message
news:EE20FFCD-EE56-47DA-9CA9-0CBED9F5640A@microsoft.com...[color=blue]
> Perhaps someone here can help me out...
>
> RegEx: "^.*\d{5}(-\d{4})?.*$"
> Intended Purpose: To strip out the City/State/ZipCode line from a
> signature.
> Sample Text:
>
> Joe Jackson
> 131 W. 5th Street
> New York, NY 10023
>
> RegEx Should Return: "New York, NY 10023"
>
> 1) This RegEx works correctly in Excel using "Microsoft VBScript Regular
> Expressions 5.5" object library
> 2) This RegEx works correctly with web-based .NET processor on
> http://www.regexlib.com/RETester.aspx
> 3) This RegEx DOES NOT WORK in .NET v1.1 (well, at least not for me!)
> 4) I found the article "FIX: The Regex class and the Match class may not
> correctly find matches for a regular expression" on Microsoft Support site
> (http://support.microsoft.com/default...;en-us;822923), however
> the
> versions of the files that they say create the fix are OLDER than the ones
> I
> have, so perhaps this is a fix for .NET v1.0. ???[/color]


BigAl
Guest
 
Posts: n/a
#7: Nov 16 '05

re: RegEx Not Working In .NET


'I've tried to extract the basics of what I'm doing below...


----------------------------------------------------------
Sub Test
Dim sText as String, sCity as String, sState as String, sZip as String
sText = "Joe Jackson" & vbCrLf & "123 Main St." & vbCrLf & "New York, NY
10023"
GetCityStateZip sText, sCity, sState, sZip
Debug.Pring sCity &","& sState & ","& sZip
End Sub

Private Sub GetCityStateZip(ByRef sText As String, ByRef sCity As String,
ByRef sState As String, ByRef sZip As String)
Const REG_EXP_ZIPCODE = "\d{5}(-\d{4})?$"
Const REG_EXP_CITY_STATE_ZIP = "^.*\d{5}(-\d{4})?.*$"

Dim sCityStateZip As String, sCityState As String

sCityStateZip = RetrieveRegExp(REG_EXP_CITY_STATE_ZIP, sText)
'Find city/state/zip
If sCityStateZip = "" Then Exit Sub

sZip = RetrieveRegExp(REG_EXP_ZIPCODE, sCityStateZip)
'Find Zip

sCity = Pop(",", sCityState) 'Parse City
sState = Trim(sCityState) 'Get State

End Sub

Function RetrieveRegExp(ByVal patrn, ByVal strng)
Dim RetStr As String
Dim iRegExOptions As Integer = ExplicitCapture + IgnoreCase +
Multiline
Dim regEx As New regEx(patrn), Match As Match, Matches As
MatchCollection ' Create variable.

Matches = regEx.Matches(strng) ' Execute search.
For Each Match In Matches ' Iterate Matches collection.
RetStr = Match.Value
Next
RetrieveRegExp = RetStr

End Function

Closed Thread