Escaping a comma

2.3k Views Asked by At

I have this regex

(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

but I'm having trouble with the comma. Escaping the comma like this \, does not solve the problem. What can I do to make this regex works work??

My code:

if (preg_match("/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/", "https://google.com/picture.jpeg")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Thanks in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

You should use single apostrophes here and escape the single apostrophe:

if (preg_match('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', "https://google.com/picture.jpeg")) {
//             ^                                                                                                                                                                        ^                 ^
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

See IDEONE demo

Otherwise, you would have to double the backslashes to actually represent literal backslashes. Note that escaping a comma is not necessary at all. You don't even have to escape the hyphen at the final position inside the character class [a-z0-9.-].

0
On

You've unescaped delimiter over here i.e two after https?:// needs to be https?:\/\/ and one over here [a-z]{2,4}/ needs to be [a-z]{2,4}\/

(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))