LowPowerLab Forum

Software support => Coding questions => Topic started by: tunkmountainman on May 24, 2020, 02:02:59 PM

Title: Learning regexp
Post by: tunkmountainman 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...
Title: Re: Learning regexp
Post by: tunkmountainman 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?
Title: Re: Learning regexp
Post by: TomWS 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.
Title: Re: Learning regexp
Post by: Felix 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.
Title: Re: Learning regexp
Post by: tunkmountainman 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.