正则表达式,用于匹配字符串后的字符串,直到逗号为止

时间:2019-04-18 18:33:26

标签: regex string regex-lookarounds regex-group regex-greedy

这是一个示例字符串。

For Each hyper_link In objCollection If vba.Lcase(hyper_link.innerText) like "*dashboard*" Then hyper_link.Click Exit For end if

我只想捕获第一个版本号,如果可能的话,不包括单词version,但不包括句点和括号。遇到逗号时,结果将停止。结果将是: "BLAH, blah, going to the store &^5, light Version 12.7(2)L6, anyway plus other stuff Version 3.3.4.6. Then goes on an on for several lines..."

我不希望它在文本中包含其他版本的实例。能做到吗?

我已经尝试过"1272L6",我知道它不能解决删除句点和括号的问题,也不能解决后续版本。

2 个答案:

答案 0 :(得分:0)

This exact RegEx,也许不是最好的解决方案,但是它可能会帮助您获得1272L6

([0-9]{2})\.([0-9]{1})\(([0-9]{1})\)([A-Z]{1}[0-9]{1})

它将创建四个组(其中$1$2$3$4是您的目标1272L6)并传递

您可以将{1}更改为其他重复次数,例如{1,2}

enter image description here

答案 1 :(得分:0)

假设版本号是固定格式,而不是特定数字或字母,则可以这样做。

  String s = "this is a test 12.7(2)L6, 13.7(2)L6, 14.7(2)L6";
  String reg = "(\\d\\d\\.\\d\\(\\d\\)[A-Z]\\d),";
  Matcher m = Pattern.compile(reg).matcher(s);
  if (m.find()) { // should only find first one
     System.out.println(m.group(1).replaceAll("[.()]", ""));
  }
相关问题