Php 我的正则表达式出了什么问题?
我只需要从下面的一行中得到这些部分:Jony、Smith和example-free@wpdevelop.com 正如你所看到的,它们介于^和~Php 我的正则表达式出了什么问题?,php,regex,Php,Regex,我只需要从下面的一行中得到这些部分:Jony、Smith和example-free@wpdevelop.com 正如你所看到的,它们介于^和~ text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~ 为此,我尝试: preg_match_all ('/\^(.*?)\~/', $row['form'], $res); 对于显示的名称:^name1^Jony~ 对于第二个名称:^secondn
text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~
为此,我尝试:
preg_match_all ('/\^(.*?)\~/', $row['form'], $res);
对于显示的名称:^name1^Jony~
对于第二个名称:^secondname1^Smith~
和电子邮件:^email1^示例-free@wpdevelop.com~
正如您所看到的,文本这个词已经消失了,但是^、name1、secondname1、email1和~
你能告诉我正则表达式出了什么问题吗?将。*改为[^^]*表示除^
您的正则表达式必须是“/\^[^\^]*?\~/”,您正在使用,。它选择^。您不需要选择^using[^\^]而不是..这样更好:
<?php
$string = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~';
preg_match_all('/\^.+?\^(.+?)~/', $string, $matches);
var_dump($matches);
我在正则表达式中添加了这个。+?^部分,它与两个字符之间的文本相匹配。您能提供更多的示例数据吗?否则,按^和^进行分解并从数组中获取值会更容易。在^和^上进行两阶段分解会更容易吗?或者更容易:preg_split/\~\\^/,$data;preg\u match\u all'/\^[^^]*?:~/',$str,$res;嗨,谢谢我添加了[^\~]以删除~是否正确:'/\^[^\^]*?[^\~]/'否,您现在也在选择~,您希望它是/^[^^]*?[^~]/
<?php
$string = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-free@wpdevelop.com~';
preg_match_all('/\^.+?\^(.+?)~/', $string, $matches);
var_dump($matches);
array(2) {
[0]=>
array(3) {
[0]=>
string(12) "^name1^Jony~"
[1]=>
string(19) "^secondname1^Smith~"
[2]=>
string(35) "^email1^example-free@wpdevelop.com~"
}
[1]=>
array(3) {
[0]=>
string(4) "Jony"
[1]=>
string(5) "Smith"
[2]=>
string(26) "example-free@wpdevelop.com"
}
}