1

I'm trying to define a route in a Phalcon Micro App, but can't figure out how to pass the global flag to the regular expression. Here is my route

/api/v1/product/detail/{sku:([\w\d]+-?)*[\w\d]*}

Expecting both of these to match, however the later requires the global modifier to match.

  • 8Z-WEXN-CG0H
  • 025-3bags

How can I specify a flag in the regular expression? I did not see this specified in the documentation.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89

1 Answers1

3

Global modifier is set whenever you don't want to stop at first match and since you have one matching value at a time you don't need g modifier either.

Let's go with an example. Suppose your current input string is this:

/api/v1/product/detail/8Z-WEXN-CG0H
/api/v1/product/detail/025-3bags

Then your regex stops at first match since no g's applied but if your input string is one of these at a time:

/api/v1/product/detail/8Z-WEXN-CG0H
/api/v1/product/detail/025-3bags

It works so you don't need g at all. And I'd suggest you to modify your regex into this simplified:

\w+(?:-\w+)*

As \w includes \d as part of a match.

revo
  • 47,783
  • 14
  • 74
  • 117
  • Thank you sir - here is the final regex that got it working in phalcon `((\w+(?:-\w+)*)`. I had to use a non-capturing group on the inner group. Does that look legit to you? Seems to be working as I test in my app... – quickshiftin Feb 02 '18 at 15:06
  • I don't see a reason for outer grouping but if that works, there you go. – revo Feb 02 '18 at 15:08