简体   繁体   中英

VBA regex look ahead

enter code here I'm trying to identify numbers that may be attached to a port after a "."
The format of the ports is ge-#/#/# (can be ge-0/0/n where 0 <= n <=23, or ge-0/1/m where 0 <= m <= 3)

Example:

ge-0/0/10.2014

The result should be "2014"

Currently, I'm using the following RegEx to identify the ports:

\bge-0/(0/([01]?[0-9]|2[0-3])|1/[0-3])\b

My problem is creating a check for the port, but only printing what's after it. My attempts to start simply and then toss in the port code are having poor results. If anyone could help create the RegEx I'm looking for and explain how it checks for the port without printing it, they would be very appreciated.

Thanks

Edit:
Only the pattern may be changed to solve this problem. This is because the pattern is being put into a txt file that will be read from by a vba macro. The vba code cannot be modified.

Expand your regex with a capturing subpattern for the additional number \\.([0-9]+) :

Dim regEx As New VBScript_RegExp_55.RegExp
regEx.Pattern = "\bge-0/(0/([01]?[0-9]|2[0-3])|1/[0-3])\.([0-9]+)\b"

Use SubMatches to retrieve the subpattern; refer to index 2, as there are two preceding pairs of parentheses in the regex.

Dim source, matches, result
Set source = "ge-0/0/10.2014"
If regEx.test(source) Then
    Set matches = regEx.Execute(source)
    Set result = matches(0).SubMatches(2)
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM