vbs正则表达式:仅匹配FIRST 2下划线之间的文本

时间:2011-05-25 17:40:45

标签: regex vba vbscript

如果文件名被称为

,我正在使用VBS进行文件名搜索
hello_world_2012_is_not the end of the world.pdf

然后正则表达式应匹配单词“world”和别的

我试过了:

[_][^_]+(?=_)

但是它获得了两个下划线之间的所有内容。我如何只选择第一次出现?

2 个答案:

答案 0 :(得分:4)

正则表达式本身应该是这样的:

_([^_]*)_

该字符串被捕获到第1组。

或者,使用字符串函数来定位前2个下划线,然后提取它们之间的内容。

答案 1 :(得分:4)

我建议使用以下正则表达式:

/^[^_]*_([^_]*)_/

说明:

^        # Anchor the search to the start of the string
[^_]*    # Match any number of non-underscores, don't capture them
_        # Match the first underscore
([^_]*)  # Match any number of non-underscores, do capture them
_        # Match the second underscore.

然后第一个捕获组($1)将包含world,并且正则表达式将不会与字符串中的任何其他位置匹配。

相关问题