PHP preg_split()-don';t在'之间拆分空格';
我有这个字符串:PHP preg_split()-don';t在'之间拆分空格';,php,preg-split,Php,Preg Split,我有这个字符串: $string = "My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?"; 我用空格和一些运算符(=,,!=,>=,正则表达式拆分这个字符串 (?:\'([^\']*[\'s]?)\'|\"([^\"]*)\")|[^\s,<>=!]+|(?:,|[<>!]?=|<>?|>) (?:\'([^\']
$string = "My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?";
我用空格和一些运算符(=,,!=,>=,正则表达式拆分这个字符串
(?:\'([^\']*[\'s]?)\'|\"([^\"]*)\")|[^\s,<>=!]+|(?:,|[<>!]?=|<>?|>)
(?:\'([^\']*[\'s]?)\“([^\']*)\”)([^\']*)\”)[^\s,=!]+(?:,[!]?=|?|?>)
您可以在此处看到匹配项:
PHP代码
$text = "My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?";
preg_match_all('/(?:\'([^\']*[\'s]?)\'|\"([^\"]*)\")|[^\s,<>=!]+|(?:,|[<>!]?=|<>?|>)/', $text, $matches);
foreach (array_filter($matches[1]) as $k => $v)
$matches[0][$k] = $v;
$text=“我的名字叫艾玛,我有个难题,‘纽约’和‘雅典娜’之间的距离是多少?”;
预匹配所有('/(?:\'([^\']*[\'s]?)\“([^\“]*)\”)预匹配所有('/((?:,[!])+(?:,[!])预匹配($text,$matches);
foreach(数组_过滤器($matches[1])为$k=>$v)
$matches[0][$k]=$v;
结果
Array
(
[0] => My
[1] => name
[2] => is
[3] => Emma
[4] => and
[5] => i
[6] => have
[7] => a
[8] => dillemma
[9] => ,
[10] => what's
[11] => the
[12] => distance
[13] => between
[14] => New York pop
[15] => and
[16] => Athene
[17] => ?
)
Array
(
[0] => age
[1] => <
[2] => 21
[3] => ,
[4] => length
[5] => >
[6] => 10
[7] => ,
[8] => height
[9] => <>
[10] => 10
[11] => ,
[12] => width
[13] => !=
[14] => 100
[15] => ,
[16] => name
[17] => =
[18] => Emma Einarsson
[19] => or
[20] => it
[21] => can
[22] => be
[23] => words
[24] => time
[25] => >=
[26] => 10
[27] => ,
[28] => clouds
[29] => <=
[30] => 4
)
数组
(
[0]=>我的
[1] =>名称
[2] =>是
[3] =>艾玛
[4] =>和
[5] =>我
[6] =>有
[7] =>a
[8] =>难题
[9] => ,
[10] =>这是什么
[11] =>
[12] =>距离
[13] =>介于
[14] =>纽约流行音乐
[15] =>和
[16] =>雅典娜
[17] => ?
)
排列
(
[0]=>年龄
[1] => <
[2] => 21
[3] => ,
[4] =>长度
[5] => >
[6] => 10
[7] => ,
[8] =>高度
[9] =>
[10] => 10
[11] => ,
[12] =>宽度
[13] => !=
[14] => 100
[15] => ,
[16] =>名称
[17] => =
[18] =>艾玛·艾纳森
[19] =>或
[20] =>它
[21]=>可以
[22]=>be
[23]=>单词
[24]=>时间
[25] => >=
[26] => 10
[27] => ,
[28]=>云
[29] => 4
)
请注意,捕获保存在数组$matches[0]中的所有数据如果我理解问题要求(在阅读了问题和许多注释之后),唯一棘手的是保留单引号的子字符串 您要隔离:
!,
=,
`
带测试组的代码()
$strings=[
“age10,height10,width!=100,name='Emma Einarsson'或者它可以是单词时间>=10,clouds 10,height 10,width!=100,name='Emma Einarsson'或者它可以是单词时间>=10,clouds可能重复的(具体地说)@iainn该答案不会将我的运算符保留在数组中:(例如,如果我有age=21
(请注意,age=和21之间没有空格)我希望将其拆分为['age','=','21']
:请更新您的问题,以便那些可能想为您提供优化解决方案的人和具有类似任务的研究人员能够清楚地了解问题。嘿@Almog!:D现在的问题是,我将所有其他键清空数组([0]=>[1]=>[2]=>[3]=>[4]=>[5]=>[6]=>[7]=>[8]=>[9]=>[10]=>[11]=>[12]=>[13]=>[14]=>[15]=>[16]=>纽约[17]=>[18]=>[19]=>雅典娜[20]=>
X你能帮我弄清楚我做错了什么吗?:-s$re='/(?:\”([^\\\\\\\\'s]*))(?:“([^\\\\\\\\\\”)*)(?:“([^\\\\\\”)*),[^\\”),[^\\\!!]/“;preg_match_all($re,$string,$matches,preg_SET_ORDER,0);var_dump($matches);
@emma's regexHei@Hammurabi!现在的问题是,它将“纽约”和“纽约”都放在该数组中而不带引号:(Hey@Almog!:D所以现在它可以与该字符串一起使用,但请看,如果我尝试在另一个字符串age>21上使用此代码,我的州是“纽约”,而我的名字是“Emma Einarsson”
我的名字不像“纽约”,而是在两个键中打断[12]=>“Emma
和[13]=>Einarsson'
…我觉得我的名字讨厌我_X@emma这可能是因为不同的正则表达式,你能给我一个所有情况的字符串,以及它需要给你什么作为例子吗?这样我就可以一次完成所有情况:P@emma这比您接受的答案更有效,但我希望您验证它是否适用与您的项目数据保持一致。看到不切实际的样本数据的问题在于,我们可能会在模式中写入不必要的组件。将来,请始终提供非常真实的输入数据(只需在需要时混淆/编辑任何私人数据).如果能更好地了解您真实数据的可变性,我或许能够改进我的答案。
[14] => New York
[16] => Athene
Array
(
[0] => My
[1] => name
[2] => is
[3] => Emma
[4] => and
[5] => i
[6] => have
[7] => a
[8] => dillemma
[9] => ,
[10] => what's
[11] => the
[12] => distance
[13] => between
[14] => New York
[15] => and
[16] => Athena
[17] => ?
)
(?:\'([^\']*[\'s]?)\'|\"([^\"]*)\")|[^\s,<>=!]+|(?:,|[<>!]?=|<>?|>)
$text = "My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?";
preg_match_all('/(?:\'([^\']*[\'s]?)\'|\"([^\"]*)\")|[^\s,<>=!]+|(?:,|[<>!]?=|<>?|>)/', $text, $matches);
foreach (array_filter($matches[1]) as $k => $v)
$matches[0][$k] = $v;
Array
(
[0] => My
[1] => name
[2] => is
[3] => Emma
[4] => and
[5] => i
[6] => have
[7] => a
[8] => dillemma
[9] => ,
[10] => what's
[11] => the
[12] => distance
[13] => between
[14] => New York pop
[15] => and
[16] => Athene
[17] => ?
)
Array
(
[0] => age
[1] => <
[2] => 21
[3] => ,
[4] => length
[5] => >
[6] => 10
[7] => ,
[8] => height
[9] => <>
[10] => 10
[11] => ,
[12] => width
[13] => !=
[14] => 100
[15] => ,
[16] => name
[17] => =
[18] => Emma Einarsson
[19] => or
[20] => it
[21] => can
[22] => be
[23] => words
[24] => time
[25] => >=
[26] => 10
[27] => ,
[28] => clouds
[29] => <=
[30] => 4
)
$strings = [
"age<21,length>10,height<>10,width!=100,name='Emma Einarsson' or it can be words time>=10,clouds<=4",
"age < 21, length > 10, height <> 10, width != 100, name = 'Emma Einarsson' or it can be words time >= 10, clouds <= 4",
"My name is Emma and i have a dillemma, what's the distance between 'New York' and 'Athene' ?",
"'New York' and London at the start and end with Paris and 'Los Angeles'"
];
foreach ($strings as $string) {
var_export(preg_match_all("~\B'\K(?:[^']+)|\b[a-z']+\b|\d+|[<>!=?]+~i", $string, $out) ? $out[0] : 'fail');
echo "\n";
}
~ #start of pattern delimiter
\B'\K(?:[^']+) #match a single-quote not preceded by [a-zA-Z0-9_], then restart the fullstring match using (\K), then match one or more non-single quote characters
| #OR
\b[a-z']+\b #match one or more letters and apostrophes
| #OR
\d+ #match one or more digits
| #OR
[<>!=?]+ #match one or more of your listed operators/symbols
~ #end of pattern delimiter
i #pattern modifier - make whole pattern case-insensitive