Regex program to find a Number with spaces and back slashes

Viewed 89

Below regex extracts integers, but one case is failing.

Example strings:

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

My regex:

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

See demo link.

Sample code:

 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")
1 Answers

You may use

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

See the regex demo

Details

  • (?:,|^|=[\w.]+\s+)
  • avg\\ shops - avg shops string
  • \s* - 0 or more whitespaces
  • = - a = symbol
  • ([0-9.]+) - Capturing group 1: one or more digits or . chars.
Related