在 Excel VBA 中使用正则表达式从文件名中提取字符
Extract Character from Filename with RegEx in Excel VBA
我的字符串是这样的文件名:
文件名 1 (A12 V1.1)
文件名 1 (V2) 文本 (A9 V2.3 8.99)
文件名 V2 文本 (A34 8.3 V4)
如何只提取以 'V' 开头的数字,仅包含在最后一个括号内,不包括 'V'?
即- 1.1、2.3、4
以下正则表达式 UDF 由 YasserKhalil 提供 (
但是,UDF 返回的文件名如下:
IPV32_20.dll 其中 returns V32-20
IMV1.dll 其中 returns V1.
这些文件名不包含括号,因此它们不应返回任何内容。
如何修改这个UDF?
我猜我们可能想在 V:
之后捕获数字
(?:.+V)([0-9.]+)(?:.+|$)
我们在这里使用两个 non-capturing 组 (?:.+V)
和 (?:.+|$)
添加左右边界,在中间,([0-9.]+)
,我们正在捕获我们想要的数字.
DEMO
EDII
根据评论,OP 似乎只想 return V 之后的数字和点,因此我们更改正则表达式以在下面提供。
以下好像是return你所描述的:
Option Explicit
Function vNum(s As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
With re
.Pattern = "\(.*V([\d.]+)(?=.*\))"
' Regex above changed from original to return only digits and dots that immediately follow the V "\(.*V(\S+)(?=.*\))"
If .test(s) = True Then
vNum = .Execute(s)(0).submatches(0)
End If
End With
End Function
下面是对正则表达式的详细描述:
请注意,由于您的描述指定的是 "string" 而不是数字,因此这可能是 return。如果您需要将其限制为数字和小数,这是一个简单的更改
提取以 'V' 开头但不包括最后一个方括号
的字符串
\(.*V(\S+)(?=.*\))
选项:区分大小写; ^$ 不匹配换行符
- Match the opening parenthesis character
\(
- Match any single character that is NOT a line break character (line feed)
.*
- Match the character “V” literally (case sensitive)
V
- Match the regex below and capture its match into backreference number 1
(\S+)
- Assert that the regex below can be matched starting at this position (positive lookahead)
(?=.*\))
- Match any single character that is NOT a line break character (line feed)
.*
- Match the closing parenthesis character
\)
创建于RegexBuddy
我的字符串是这样的文件名:
文件名 1 (A12 V1.1)
文件名 1 (V2) 文本 (A9 V2.3 8.99)
文件名 V2 文本 (A34 8.3 V4)
如何只提取以 'V' 开头的数字,仅包含在最后一个括号内,不包括 'V'?
即- 1.1、2.3、4
以下正则表达式 UDF 由 YasserKhalil 提供 (
但是,UDF 返回的文件名如下:
IPV32_20.dll 其中 returns V32-20
IMV1.dll 其中 returns V1.
这些文件名不包含括号,因此它们不应返回任何内容。
如何修改这个UDF?
我猜我们可能想在 V:
之后捕获数字(?:.+V)([0-9.]+)(?:.+|$)
我们在这里使用两个 non-capturing 组 (?:.+V)
和 (?:.+|$)
添加左右边界,在中间,([0-9.]+)
,我们正在捕获我们想要的数字.
DEMO
EDII 根据评论,OP 似乎只想 return V 之后的数字和点,因此我们更改正则表达式以在下面提供。
以下好像是return你所描述的:
Option Explicit
Function vNum(s As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
With re
.Pattern = "\(.*V([\d.]+)(?=.*\))"
' Regex above changed from original to return only digits and dots that immediately follow the V "\(.*V(\S+)(?=.*\))"
If .test(s) = True Then
vNum = .Execute(s)(0).submatches(0)
End If
End With
End Function
下面是对正则表达式的详细描述:
请注意,由于您的描述指定的是 "string" 而不是数字,因此这可能是 return。如果您需要将其限制为数字和小数,这是一个简单的更改
提取以 'V' 开头但不包括最后一个方括号
的字符串\(.*V(\S+)(?=.*\))
选项:区分大小写; ^$ 不匹配换行符
- Match the opening parenthesis character
\(
- Match any single character that is NOT a line break character (line feed)
.*
- Match the character “V” literally (case sensitive)
V
- Match the regex below and capture its match into backreference number 1
(\S+)
- Assert that the regex below can be matched starting at this position (positive lookahead)
(?=.*\))
- Match any single character that is NOT a line break character (line feed)
.*
- Match the closing parenthesis character
\)
- Match any single character that is NOT a line break character (line feed)
创建于RegexBuddy