Regex 正则表达式程序,用于查找带有空格和反斜杠的数字

Regex 正则表达式程序,用于查找带有空格和反斜杠的数字,regex,scala,Regex,Scala,下面的正则表达式提取整数,但有一个例子失败了 示例字符串: Monday,City=Nice,total\ avg\ shops=350,avg\ shops=20 month=jan Monday,City=Nice total\ avg\ shops=23 avg\ shops=10 month=Feb Monday,City=Nice total\,avg\ shops=30 avg\ shops=50 month=Feb Monday total\,avg\,shops=30 avg\

下面的正则表达式提取整数,但有一个例子失败了

示例字符串:

Monday,City=Nice,total\ avg\ shops=350,avg\ shops=20 month=jan
Monday,City=Nice total\ avg\ shops=23 avg\ shops=10 month=Feb
Monday,City=Nice total\,avg\ shops=30 avg\ shops=50 month=Feb
Monday total\,avg\,shops=30 avg\ shops=50,City=Nice month=Feb
Region,Network=myTelco total\ avg\ shops=30.0 avg\ shops=20color=Pink 1590424200000000000
我的正则表达式:

(?:(?<![^,])|=\w+\s+)avg\\ shops\s*=([0-9.]+)
(?:(?)?
看

示例代码:

 val matcherInt = Pattern.compile(s"(?:(?<![^,\\s])|=\\w+\\s+)$targetz\\s*=([0-9.]+)").matcher(valueInt)
     if (matcherInt.find()) {
      println( matcherInt.group(1))
     } else
      println("Nothing match")
val matcherInt=Pattern.compile(s)(?:(?您可以使用

(?:,|^|=[\w.]+\s+)avg\\ shops\s*=([0-9.]+)

详细信息

  • (?:,| ^ |=[\w.]+\s+)
  • avg\\shops
    -
    avg shops
    string
  • \s*
    -0个或更多空格
  • =
    -一个
    =
    符号
  • ([0-9.]+)
    -捕获组1:一个或多个数字或
    字符

请指定具有当前输出和预期输出的故障情况output@Anshuman很明显,在演示链接中,
区域,Network=myTelco total\avg\shops=30.0 avg\shops=20color=Pink 15904242000000000000
没有匹配项。是否会发生灾难性的回溯问题?@santanumohanty否,它不能发生在这里:
/字符串的开头/
=
+单词或点字符+空格字符,然后是
avg\shops
,然后是0个或更多的空格,然后是
=
,然后是1个或更多的数字或点-正如您所看到的,没有出现可能从同一位置开始的模式,随后的模式都匹配不同类型的文本。请检查一下,为什么不匹配ched@santanumohanty因为在
之后不仅仅是单词或点字符,如果我得到了像-%etc这样的特殊字符,那么它就会失败,例如Region\name=Provence abc\Alpes\Cote\d'Azu