Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

PHP正则表达式中,如何匹配反斜杠

想匹配指定目标中的'123',但不成功,为什么?如果使用.作为正则表达式是是可以匹配成功的,是不是我的写法问题?代码如下:

$str = '123\123';
$pattern = '/123\/';
preg_match($pattern,$str,$arr);
print_r($arr);

原本以为上面的代码能够匹配出'123'这个结果出来,但实际上匹配不出,不知为何?反而用下面的代码就能匹配出

$str = '123123';
$pattern = '/123./';
preg_match($pattern,$str,$arr);
print_r($arr);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<?php
$str = '123\123';
$pattern = '/123\\/';
preg_match($pattern,$str,$arr);
print_r($arr);
//2\在''中表示一个

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...