RegEx Not Working In .NET

G

Guest

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.aspx?scid=kb;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. ???
 
R

Robby

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
 
D

Doug Holton

BigAl said:
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"

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"]
 
R

Robby

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
 
H

Hollenho

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
 
G

Guest

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top