简体   繁体   English

VBA正则表达式展望未来

[英]VBA regex look ahead

enter code here I'm trying to identify numbers that may be attached to a port after a "." enter code here我试图识别在“。”之后可能附加到端口的数字。
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) 端口的格式为ge-#/#/#(可以是ge-0 / 0 / n,其中0 <= n <= 23,或ge-0 / 1 / m,其中0 <= m <= 3)

Example: 例:

ge-0/0/10.2014

The result should be "2014" 结果应为“ 2014”

Currently, I'm using the following RegEx to identify the ports: 当前,我正在使用以下RegEx标识端口:

\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. 如果有人可以帮助创建我正在寻找的RegEx并解释其如何在不打印的情况下检查端口,则将不胜感激。

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. 这是因为该模式已放入txt文件中,该文件将由vba宏读取。 The vba code cannot be modified. vba代码无法修改。

Expand your regex with a capturing subpattern for the additional number \\.([0-9]+) : 用捕获子模式扩展正则表达式以获取附加数字\\.([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; 使用SubMatches检索子模式; refer to index 2, as there are two preceding pairs of parentheses in the regex. 请参阅索引2,因为正则表达式中有两对前面的括号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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