开发者

using php preg_match,warning

开发者 https://www.devze.com 2023-04-12 22:00 出处:网络
regex:^([^\\/])/([A-Z])([^\\/]+)/([^\\/]+)/$ string to match: http://127.0.0.1:8008/BeiJing/FangChan/ I use preg_match() to test
regex:^([^\/])/([A-Z])([^\/]+)/([^\/]+)/$

string to match: http://127.0.0.1:8008/BeiJing/FangChan/

I use preg_match() to test

if (preg_match('/^([^\/])/([A-Z])([^\/]+)/([^\/]+)/$/',"http://127.0.0.1:8008/BeiJing/FangChan/",$match))
    print $match[0];
else
    print 'not match';

but get a Warning: preg_match() [function.preg-match]: Unknown modifier '(' Can anyone help me write this regex so it's valid for the preg_match function? I've tried a ton of different ideas but I'm really not开发者_高级运维 too knowledgeable with regex.


/ is what is indicating the start and the end of the pattern in your example, whatever comes after the pattern is a modifier.

So this:

/^([^/])/([A-Z])([^/]+)/([^/]+)/$/

Gets read as this:

/^([^/])/

With modifiers ([A-Z])([^/]+)/([^/]+)/$/

You have to escape all /'s or change the start and end indicator:

#^([^/])/([A-Z])([^/]+)/([^/]+)/$#


You need to escape the / you use by putting a \ in front of it. Or change the first and last / to something else like:

if (preg_match('|^([^/])/([A-Z])([^/]+)/([^/]+)/$|',"http://127.0.0.1:8008/BeiJing/FangChan/",$match)) print $match[0]; else

But this regexp will not match the url.

0

精彩评论

暂无评论...
验证码 换一张
取 消