2
preg_match('`/`', '/hello/how are you/now/4/details')

The above and below returns true. Problem is I need only the below comparison to return true.

preg_match('`/hello/how are you/now/\d+/details`', '/hello/how are you/now/4/details')

I have a list of regex patterns and I need to see if the input/needle matches exactly one of the regex as illustrated above

Help.

2 Answers 2

2

The function is looking for a match for the pattern in the string. / is there so it returns true. Use ^ at the beginning of the pattern to match the beginning of the string. Use $ at the end to indicate end of string.

2
  • You mean something like this? preg_match('^/$', '/')
    – Benz
    Commented Dec 17, 2013 at 10:27
  • Use .+ or .*. . covers for any character, but only once. Repetition operators tell the regex engine to use the . more than once. * is 0 or more times, + is 1 or more.
    – JNF
    Commented Dec 17, 2013 at 20:13
0

First one will returns true because it finds / in the given string. If you want specific string match, as @JNF said use ^ and $ for beginning and ending of the string. To test your regex use https://www.debuggex.com it really helps you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.