Solved

Regular Expression Not working in VB.net

Posted on 2009-07-06
5
147 Views
Last Modified: 2012-05-07
I have posted my code below.
When I use the regex below, along with the text I define in "body", it works when I try it online at http://gskinner.com/RegExr/

VB.net doesnt find any matches though.

can anyone tell me what I need to do so that this regex will work in vb?
Dim body As String = "Customer Information:" & vbNewLine & "Bob Jones" & vbNewLine & "Phone: 442-493-2935"
 
Dim reg3 As New System.Text.RegularExpressions.Regex("(?<=customer information|customer information:)\s*\w*\s\w*", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        matches = reg.Matches(body)
        If matches.Count > 0 Then
            Dim result As String = matches(0).Value.Trim(" ")
             Return result
        End If

Open in new window

0
Comment
Question by:BobBarker_99
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24787318
The following returns the matched text. The ^\r at the end will prevent the last carriage return from being picked up. You can use ^\n, but you will get a carriage return character in your match.
(?<=Customer Information:)\s*[^\n]*\n[^\r]*

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24787326
The above is assuming you have a newline after your phone number in regards to capturing the carriage return.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24787339
just wondering if there is any typo:
Dim reg3 As New System.Text.RegularExpressions.Regex("(?<=customer information|customer information:)\s*\w*\s\w*", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        matches = reg.Matches(body)

You are declaring 'reg3' and then calling 'reg'.Matches
shouldn't that be matches = reg3.Matches(body)
0
 

Author Comment

by:BobBarker_99
ID: 24787362
Wow I'm very slow today.  It was just a typo (reg vs reg3)!!!
0
 

Author Closing Comment

by:BobBarker_99
ID: 31600258
I am an idiot
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
Visual Studios 1 76
vb.net class 3 21
VB.Net - Not Sure if Multi-threading is What I Need 4 30
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question