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.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean something like this? preg_match('^/$', '/')
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.