New to RegEx... help me solve a problem...

B2Ben

Well-known member
Joined
Aug 17, 2006
Messages
52
Programming Experience
Beginner
Here's a simplified example of what I'm working with:

VB.NET:
'Imports System.Text.RegularExpressions
Dim RE As RegEx
Dim Text As String = "xxA01BA02BA03B"
RE = New Regex("^(.*)(A[0-9][0-9]B)") 'The Pattern
Dim Extract As String = RE.Replace(Text, "$2")
MsgBox(Extract)
What I want to do:
- ignore zero-or-more non-matching characters at the beginning (the "xx")
- extract the first occurance of something that looks like A##B, where ## is a two-digit number

The problem:
In the above example, the value returned is actually the last occurrance. Specifically, "A03B". I wanted to get "A01B".

Can anybody help me learn how to get the first occurance that I want?

Thanks!
 
Last edited by a moderator:
May be can try out this

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] RE [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions.Regex
RE = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions.Regex("^(.*)(A[0-9][0-9]B)", System.Text.RegularExpressions.RegexOptions.Multiline)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Text [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "xxA01BA02BA03B"
MsgBox(RE.Matches(Text, "(A[0-9][0-9]B)").Item(1).Value)
[/SIZE]
 
lingsn, Matches is a Shared method, also Item(1) is the second match.

B2Ben, not sure what you are doing with replace here, but I don't think it is possible with a single expression to replace only first match. Using a regex expression in a lookbehind is not allowed, so you won't find this single 'a00b' where start is '^.*?'. That is the lazy character range you were seeking, by the way, but it can't be used.

Here is use of the Matches function that get's the first match:
VB.NET:
        Dim Text As String = "xxA01BA02BA03B"
        Dim expression As String = "A\d{2}B"
        Dim mc As MatchCollection = Regex.Matches(Text, expression)
        If mc.Count > 0 Then MsgBox(mc(0).Value)
This is one of the better sites about regular expressions: http://www.regular-expressions.info
 
Thanks everyone, I'll give it a try again today.

Maybe you can help me with the next step. First, a little background. I'm building an app that receives commands over the serial port as a steady stream of text. For example, let's say a command looks like A00B, where the 00 can be any 2 digit number. However, there is other text in the stream (noise or otherwise) that I don't want.

I want to use Regular Expressions to monitor my buffer as text comes streaming in. Here are a few examples of what I may see in my buffer:

One command: A00B
Data to extract: A00B

unrecognized data plus one command: xxA00B
Data to extract: A00B

multiple commands: A00BA01BA02B
Data to extract: A00B

The next step is to remove the command, and remove any preceding unrecognized data. For example:

1) Buffer: xxA00BA01BA02B
2) Extract first command from buffer (A00B)
3) Update the buffer, removing the first command we extracted and anything preceding it. Buffer now becomes "A01BA02B"

How can I accomplish step #3 above?
 
..continuing the example:
VB.NET:
Dim reminding As String = Text.Substring(mc(0).Index + mc(0).Length)
This will return the substring of original text after the first match.
 
Back
Top