Jefferson Hudson
Jefferson Hudson

Reputation: 738

VBS RegExp Error - Pulling text from beginning of line

I need to pull the first 3 to 13 characters out of a string, starting at the beginning of the line and ending at the first encounter of whitespace.

The following code I cannot get to work. I often get an "Invalid Procedure" error. Changing the pattern sometimes makes the error go away, but it never results in the match that I need.

Please Help.

EDIT: I found this pattern "/[^ ]*/" which should do what I want (I think), but now when I execute the code I get an error at line strOut = match.SubMatches(0). Any idea why?

    ' Set the pattern to find the data we would like to test; 
re_Name.Pattern = "^xe\-1/1/1"
re_Name.IgnoreCase = True
re_Name.Multiline = True

vLines = Split(strNames, vbcrlf)
For Each strLine in vLines
Set matches = re_Name.Execute(strLine)
    For Each match in matches
        strOut = match.SubMatches(0)
        MsgBox strOut
        Next
    Next

Examples of what I am searching for:

xe-0/1/1
xe-0/3/2.0
ge-11/2/1
ae0
ae0.3156

Example of what it might like in context:

xe-0/0/0        up    up   xxxxxxxxxxJxx0/0xToxBBxxx1 x;
ae0.202         up    up   ;xxxxxx1;xxxxxxxxixxxxx 
ge-11/3/0.0     up    down Rxxxxxxx RESERVEDing

Upvotes: 0

Views: 236

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

Your Regexp lacks capturing () to make Submatches possible. Using Split on space would probably a less risky approach.

Upvotes: 1

MikeM
MikeM

Reputation: 13641

I think strOut = match.SubMatches(0) should be strOut = match.Value as you're not capturing any submatches because you're not using brackets to capture anything.

If you just want to match characters up to the first space you could use

 re_Name.Pattern = "^\S+"

\S matchs any non-space character.

Upvotes: 2

Related Questions