问题描述
|
我正在用VBS进行一些文件名抓取,如果文件名被调用
hello_world_2012_is_not the end of the world.pdf
那么正则表达式应该与单词“ world”匹配,其他都不能匹配。
我尝试了这个:
[_][^_]+(?=_)
但是它得到的是介于两个下划线之间的所有内容。如何只选择第一次出现?
解决方法
我建议以下正则表达式:
/^[^_]*_([^_]*)_/
说明:
^ # 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
,并且正则表达式与字符串中的其他任何地方都不匹配。
,正则表达式本身应如下所示:
_([^_]*)_
该字符串被捕获到组1中。
或者,使用字符串函数找到前两个下划线,然后提取它们之间的内容。