How to ‘convert’ any given regex to a PHP `preg_match`-compatible regex?

Okay, this may be a dumb question, but I’m pretty new to regular expressions and I honestly have no idea how to do this.

I don’t know how to tell if a regex will work with PHP’s preg_match() or not.

For example, I would like to use the following regex with PHP’s preg_match().

\b
# Match the leading part (proto://hostname, or just hostname)
(
    # ftp://, http://, or https:// leading part
    (ftp|https?)://[-\w]+(\.\w[-\w]*)+
  |
    # or, try to find a hostname with our more specific sub-expression
    (?i: [a-z0-9] (?:[-a-z0-9]*[a-z0-9])? \. )+ # sub domains
    # Now ending .com, etc. For these, require lowercase
    (?-i: com\b
        | edu\b
        | biz\b
        | gov\b
        | in(?:t|fo)\b # .int or .info
        | mil\b
        | net\b
        | org\b
        | [a-z][a-z]\b # two-letter country codes
    )
)

# Allow an optional port number
( : \d+ )?

# The rest of the URL is optional, and begins with / . . . 
(
     /
     # The rest are heuristics for what seems to work well
     [^.!,?;"'<>()\[\]{}\s\x7F-\xFF]*
     (?:
        [.!,?]+  [^.!,?;"'<>()\[\]{}\s\x7F-\xFF]+
     )*
)?

preg_match($regex, $url); doesn’t work when the above regex is used as-is. Why not? What are the steps to take here to ‘convert’ it so that it will work?

Note that the regex I’m providing here is just an example; I’d love to learn how to convert any regex to its preg_match-compatible equivalent.

Thanks in advance!

P.S. I’m asking because I’m collecting and comparing different URL regexes on this test page: http://mathiasbynens.be/demo/url-regex People keep sending me regexes in other languages, and I don’t know how to make them work :(

Answers


You can use the x modifier flag in PHP to allow the use of whitespace and comments. See http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

Also you need to wrap the regex in a set of delimiters. So /regex/modifiers, like this:

/[abc]/xi

…the i modifier being for case insensitivity.

I highly recommend 3rd edition of Mastering Regular Expressions (3rd edition includes a whole chapter on PHP but the whole book is very enlightening!).

P.S. RegexBuddy (Windows application) can convert regexes between languages for you: http://cl.ly/050z3e1Z3e050M3W2u2a Sadly, there’s no Mac version.


Need Your Help

Issue in UITextField add inside UIImageView in iPhone?

iphone ios uiimageview uitextfield

Currently i am working in iPhone app, Using UIImageView to set a background image on the screen, then add UITextField inside the UIImageView, then i run the app, the UITextField cannot enabled, How...

How to select a range of rows using two variables in VBA

vba excel-vba range

I'm brand new to VBA and am trying to write a macro to pull specific data from all workbooks in the same directory into a master workbook. I'm getting stuck when trying to select a range of data us...

Nusoap use existing WSDL how to?

php soap wsdl nusoap

so I have a WSDL I've been giving as documentation for a soap service. I need to implement this service exactly as defined and I don't want to rewrite the wsdl using nusoap. Is there a way to tell ...