Author Topic: Learning regexp  (Read 306 times)

tunkmountainman

  • Newbie
  • *
  • Posts: 8
  • Country: us
Learning regexp
« on: May 24, 2020, 02:02:59 PM »
    Just learning regexp using "Online regex tester and debugger". Tried the regexp from "Watermeter.js" from the new "Metrics"
The tester program does not like the   regexp:/GPM\:([\d\.]+)/i,    It shows the forward slashes as needing to be escaped. Tried several other languages but only Python liked the forward slashes. I know this regexp works because it is used all over in other scripts. What am I doing incorrectly?  There should be an escaped colon after GPM...
« Last Edit: May 24, 2020, 02:08:45 PM by tunkmountainman »

tunkmountainman

  • Newbie
  • *
  • Posts: 8
  • Country: us
Re: Learning regexp
« Reply #1 on: May 25, 2020, 11:07:02 AM »
Just tried  in the "online regex tester and debugger" and it hates the forward slashes, tells me they need to be escaped.

F : { name:'F', regexp:/\bF\:(-?\d+\.\d+)\b/i, value:'', duplicateInterval:3600, unit:'', pin:1, graph:1, graphValSuffix:'F', graphOptions:{ legendLbl:'Temperature', lines: { lineWidth:1 } }},

What am I doing incorrectly?

TomWS

  • Hero Member
  • *****
  • Posts: 1924
Re: Learning regexp
« Reply #2 on: May 25, 2020, 04:30:37 PM »
You need to remove the leading "regexp:".  It is not part of the matching expression so it's thinking the forward slashes are some kind of escape character.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6279
  • Country: us
    • LowPowerLab
Re: Learning regexp
« Reply #3 on: May 26, 2020, 10:43:04 AM »
In Javascript regular expressions variable types do not have any quotes like strings but instead are defined within forward slashes '/' and followed by special designators like case insensitive (the i) etc. In JSON notation, which is found in the metric definitions, a regex variable is defined as:

variableName followed by colon (:) followed by the regular expression itself, ex:

Code: [Select]
regexp:/GPM\:([\d\.]+)/i

You can test these expressions at regexr.com where they have a very nice UI that helps you with debugging and building/modifying a regex.

tunkmountainman

  • Newbie
  • *
  • Posts: 8
  • Country: us
Re: Learning regexp
« Reply #4 on: May 28, 2020, 02:58:14 AM »
    Thank you guys you really helped. I have a lot more testing to do. Thank you again.