3

I'm having the following regular expression:

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

but it's giving me the following error:

Unknown modifier '\'

I tried escaping the backslashes like this:

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

Without any luck.. I also tried replacing the backslashes with tildes, again without luck. I've searched the internet and SO for any details about the '\' as unknown modifier but again without luck. What is going wrong here?

A code sample included on request:

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

Thanks!

Toto
  • 89,455
  • 62
  • 89
  • 125
Fabian Pas
  • 444
  • 5
  • 18

3 Answers3

7

Just escape the single quote using a backslash like this

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

EDIT :

Regarding Unknown Modifier Warning..

I assume that you may have got that on your preg functions.

A preg pattern needs a pair of characters which delimit the pattern itself.

You should add a delimiter sort of thing see here...

$body = preg_replace("/(.*)<!-- start -->(.*)<!-- end -->(.*)/","$2",$body);
                ------^                                 -----^

Source

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
5

You have to add delimiter arround your regex:

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

As delimiter, you can use almost any character except spaces or word character.
I recommend you to use a character that is not present in your regex, so you haven't to escape it.
Here I'm using ~

Toto
  • 89,455
  • 62
  • 89
  • 125
3

Since I am missing an answer that explains the warning, I add another one.

The preg functions need a regex delimiter and you forgot to put one explicitly, so the first character in the regex string is used as delimiter. Since it is an opening bracket, the according delimiter is the closing bracket.

$regex = '(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'\".,<>?«»“”‘’]))';
          |  || 
          |  || first character after the closing delimiter is an unknown modifier ==> your warning !
          |  | first closing bracket is the other delimiter
          | first opening bracket is the delimiter

That is because as regex delimiters also a pair of brackets can be used. See php doc on Delimiters

So the solution is to put an appropriate regex delimiter, like the other people here showed you.

stema
  • 90,351
  • 20
  • 107
  • 135