【问题标题】:vbs regex: match the text between ONLY the FIRST 2 underscoresvbs 正则表达式:仅匹配前 2 个下划线之间的文本
【发布时间】:2011-05-25 17:40:44
【问题描述】:

如果文件名被调用,我正在使用 VBS 进行文件名抓取

hello_world_2012_is_not the end of the world.pdf

那么正则表达式应该匹配单词“world”并且没有别的

我试过这个:

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

但它会获取两个下划线之间的所有内容。如何只选择第一个匹配项?

【问题讨论】:

  • 你想知道文件名中是否包含“世界”这个词吗?
  • 对不起...看到标题的答案
  • 好点。不,我想提取它,所以它可以是任何东西……恐怕不像寻找“世界”那么容易。

标签: regex vba vbscript


【解决方案1】:

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

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

解释:

^        # 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,并且正则表达式不会匹配字符串中的任何其他位置。

【讨论】:

  • 太棒了!无需使用字符串操作符来提取下划线。非常感谢
【解决方案2】:

正则表达式本身应该如下所示:

_([^_]*)_

字符串被捕获到组 1。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2019-03-21
    相关资源
    最近更新 更多