Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式,用于在字符串后匹配字符串,直至逗号_Regex_String_Regex Lookarounds_Regex Group_Regex Greedy - Fatal编程技术网

Regex 正则表达式,用于在字符串后匹配字符串,直至逗号

Regex 正则表达式,用于在字符串后匹配字符串,直至逗号,regex,string,regex-lookarounds,regex-group,regex-greedy,Regex,String,Regex Lookarounds,Regex Group,Regex Greedy,下面是一个示例字符串 “废话,废话,去商店&^5,轻版12.7(2)L6,不管怎样 再加上3.3.4.6版的其他内容。然后继续进行几行… 如果可能的话,我只想捕获第一个版本号,不包括单词version,但不包括句点和括号。结果将在遇到逗号时停止。结果将是: “1272L6” 我不希望文本中包含其他版本的实例。这能做到吗 我尝试了(?,可能不是最好的解决方案,但它可能会帮助您获得1272L6: ([0-9]{2})\.([0-9]{1})\(([0-9]{1})\)([A-Z]{1}[0-9]{1

下面是一个示例字符串

“废话,废话,去商店&^5,轻版12.7(2)L6,不管怎样
再加上3.3.4.6版的其他内容。然后继续进行几行…

如果可能的话,我只想捕获第一个版本号,不包括单词version,但不包括句点和括号。结果将在遇到逗号时停止。结果将是:
“1272L6”

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

我尝试了
(?,可能不是最好的解决方案,但它可能会帮助您获得
1272L6

([0-9]{2})\.([0-9]{1})\(([0-9]{1})\)([A-Z]{1}[0-9]{1})
它创建四个组(其中
1$2$3$4
是您的目标
1272L6
)并通过、)和(

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


假设版本号固定在格式上,而不是固定在特定的数字或字母上,您可以这样做

  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("[.()]", ""));
  }

它可以通过两个步骤完成:1)提取你需要的子串2)从中去除非无晶体的焦碳。你不能用一个匹配操作来匹配不相交的字符,我怀疑你可能会使用正则表达式替换操作来捕获组/反向引用,除非匹配的格式总是相同的。版本的格式有多严格?如果没有确切的语法,很难指定解决方案。也可能是12.7(23)L6或12(2.7)L62,等等?