2

My code below loosely is per Zehra Nasif answer from Regular expression for matching latitude/longitude coordinates? (May 15 '14 at 20:00). While my code below is for MapServer, I think vanilla RE should work; the documentation says 'Google Regular Expression'! Here is my code:

VALIDATION
    # '^[0-9]{5,8}$' # %parcelid% must be numeric and between 5 and 8 characters
    'memberlong' "^-?[0-9]{1,3}(?:\.[0-9]{1,30})$" # -91.6666 
    'memberlat' "^[0-9]{1,3}(?:\.[0-9]{1,30})$" # 30.6666
    'num_miles' "[0-9]+$"    #  1200 works. 
 END

The first line is per the Mapserver document--that's just to illustrate their engine. And the num_miles works when the input argument is 1200; that's also just to illustrate their engine. However, both memberlong and memberlat (or at least one of them--can't be sure because debug not pinpointing that) throw error msApplySubstitutions(): Regular expression error. Parameter pattern validation failed and I think my regular expressions are not correct. I think, for now at least, it is safe to assume that memberlong (the longitudes) will be always negative and memberlat (the latitudes) will always be positive values, although for long term they should be able to handle negatives/positive. I just need to move on.

Any ideas?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
IrfanClemson
  • 1,699
  • 6
  • 33
  • 52
  • How can `num_miles` work to match 1200? It matches 1 digit. It should be `[0-9]+` (or `\d+`) – cornuz May 25 '21 at 18:04
  • Yes, it does work. I think it just expects any number between 0-9. – IrfanClemson May 25 '21 at 18:06
  • 1
    It expects any single digit between 0 and 9. I can assure you that `[0-9]` does not match 1200. I think in this case it worked because it matched 1 digit, and that was good enough for how the test is done. But it did not match 1200. The correct expression is `^[0-9]+$` – cornuz May 25 '21 at 18:07
  • I don't know how, to be honest. But look at their sample code posted for parcelid--same logic except restricting to 5-8 characters. – IrfanClemson May 25 '21 at 18:09
  • You were correct about num_miles; doing only [0-1] still wokred. So I am going to edit my original post... Thank you. – IrfanClemson May 25 '21 at 18:17
  • Good question. I am very new to MapServer. I think the expressions are in the VALIDATION block of a .MAP file; validation is being enforced to prevent SQL injections. After the validation passes then data is sent to a PostgreSQL Server. I do know that if I were to make 'memberlong' to string in validation phase then validation would pass except database would interpret that as column name, instead of a value being passed to the query. – IrfanClemson May 25 '21 at 18:23
  • Looking at the documentation (probably you should have included a [link](https://mapserver.org/mapfile/validation.html) ), this seems correct to me. It expects regexes to match. And they are valid regexes, at least in the most common dialects. However the grouping `(?:)` is not necessary. – cornuz May 25 '21 at 18:26
  • Sorry, working off a local PDF version of document. Will be mindful next time. Thanks for the suggestion and for the help. – IrfanClemson May 25 '21 at 18:27
  • 1
    You need no non-capturing groups, `'memberlong' "^-?[0-9]{1,3}\.[0-9]{1,30}$"` and `'memberlat' "^[0-9]{1,3}\.[0-9]{1,30}$"` should work. – Wiktor Stribiżew May 25 '21 at 20:41
  • That looks the same as I already have? Anyway, I just copied/pasted your to replace my variables and still the same error? Thank you. – IrfanClemson May 25 '21 at 20:55
  • This is what I have now: 'memberlong' "^-?[0-9]{1,3}\.[0-9]{1,30}$" 'memberlat' "^[0-9]{1,3}\.[0-9]{1,30}$" – IrfanClemson May 25 '21 at 20:57
  • wait.. double checkimg – IrfanClemson May 25 '21 at 21:12
  • @WiktorStribiżew your answer is working. Please post that as Answer. Thank you! – IrfanClemson May 25 '21 at 21:33

1 Answers1

1

Not all regex flavors support non-capturing groups, (?:...) constructs. For example, POSIX ERE/BRE and XML Schema regex flavors only support capturing groups.

Moreover, in your case, you may simply remove the non-capturing groups as (?:xxx) = xxx. We only use non-capturing groups when we need to introduce alternations, or when we need to repeat a sequence of patterns with a quantifier.

So you need to use

'memberlong'    "^-?[0-9]{1,3}\.[0-9]{1,30}$"
'memberlat'     "^[0-9]{1,3}\.[0-9]{1,30}$"
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563