Home > Mobile >  Regex to exclude URL with special configuration
Regex to exclude URL with special configuration

Time:05-19

I want to exclude a url with this configuration https://subdomain.example.com/y22la7hv from a string.

I'm trying to do this with the following regex but is not working: https://subdomain\.example\.com/([a-zA-Z] ([0-9] [a-zA-Z] ) )

Where am I failing?

CodePudding user response:

Here it is after lot's of trying and error. Enjoy!

$str = 'https://subdomain.example.com/y22la7hv';
$pattern = '/https:\/\/subdomain\.example\.com\/([a-zA-Z] ([0-9] [a-zA-Z] ) )/i';
$exclude_rgx = preg_replace($pattern, '', $str);

CodePudding user response:

You miss one backslash ("\"), just after ".com"

\.example\.com\/([a-zA-Z] ([0-9] [a-zA-Z] ) )

CodePudding user response:

If you are using the generally accepted standard of forward slashes to surround the regex, you will need to escape the http:// and .com/ parts. For instance:

https:\/\/subdomain\.example\.com\/([a-zA-Z] ([0-9] [a-zA-Z] ) )

View this working on PHP Live Regex.

And results in the following matches:

array(
    0   =>  https://subdomain.example.com/y22la7hv
    1   =>  y22la7hv
    2   =>  7hv
)
  • Related