Pamela Sillah Pamela Sillah - 19 days ago 6
PHP Question

WordPress regex not working as expected

I need to put a string like

user-profile/id=3
into regex form. I tried
'user-profile/id=\d+$'
and also
'user-profile/([a-z][.][0-9]+)/?$'
, but none of them are working. What is the correct way?

Answer

Your problem is that you aren't escaping the / character with a backslash.

Another problem you could face immediately after solving that one, is that you are using the $ character, which means end of line. If there are more characters afterwards, even just a single space, then it won't match.

If you try:

user-profile\/(id=\d+)

You'll probably find that it matches just fine. The brackets I added in will capture id=3 in capture group #1.