[英]regex string between two characters from end php
Im trying to get a part of string doing regex. 我试图让字符串做正则表达式的一部分。 For eg.
例如。
$input = "This is a 'wonderful' day except i am 'stuck' here"
I want to get all characters between two '
s. 我想这两者之间的所有字符
'
秒。
for this i'm using 为此,我正在使用
preg_match('~\'(.*?)\'~', $input, $output);
but the result i'm getting is only wonderful
in $output[0]
但是我得到的结果仅在
$output[0]
wonderful
what i'm doing wrong? 我做错了什么? how to get the second part ie
stuck
in this example? 如何获得第二部分,即
stuck
在此示例中?
EDIT: I asked this question after checking $output[1]. 编辑:我在检查$ output [1]后问了这个问题。 '
stuck
' is not there! “
stuck
”不存在!
also apart from testing it from my program, i also tried an online regex tester. 除了从我的程序中对其进行测试之外,我还尝试了一个在线正则表达式测试器。 here's the result:
结果如下:
http://s30.postimg.org/g6dj5xvmp/Selection_009.png http://s30.postimg.org/g6dj5xvmp/Selection_009.png
Do like this 像这样
<?php
$str = "This is a 'wonderful' day except i am 'stuck' here";
preg_match_all("/'(.*?)'/", $str, $matches);
print_r($matches[1]);
OUTPUT : 输出:
Array
(
[0] => wonderful
[1] => stuck
)
This is really quite simple. 这真的很简单。 You're using
preg_match
, which attempts to find one occurance of a given pattern, to find all matches use preg_match_all
. 您使用
preg_match
,它试图找到一个给定的模式之一次数,找到所有比赛使用preg_match_all
。
Both work in the same way: the $matches
array will have the full pattern-match assigned to index 0 (including the quotes), and the group(s) will be assigned to all subsequent indexes (in this case $matches[0]
will contain the chars inside the quotes). 两者的工作方式相同:
$matches
数组将具有分配给索引0(包括引号) 的完整模式匹配 ,并且组将分配给所有后续索引(在这种情况下, $matches[0]
将包含引号内的字符)。 The difference is that preg_match_all
will assign arrays to the aforementioned indexes, listing each match for the pattern. 区别在于
preg_match_all
将为上述索引分配数组 ,列出该模式的每个匹配项。
preg_match("/'([^]+)'/", $input, $matches);
var_dump($matches);
will give an array like this: 将给出这样的数组:
array(
"'wonderful'", //because the pattern mentions the ' chars
"wonderful" //because I'm grouping the chars inside the '
);
Whereas this code: 而此代码:
preg_match_all("/'([^']+)'/", $input, $matches));
Gives you: 给你:
array (
//array of full matches, including the quotes
array (
'\'wonderful\'',
'\'stuck\'',
),
//array with groups
array (
'wonderful',
'stuck',
),
);
As you can see on this live example 如您在此实时示例中所见
I've simplified your expression a little, because you're interested in what's "delimited" by single quotes, hence I match and group every non '
that follows a single quote and that is, in turn followed by another single quote. 我对表达式进行了一些简化,因为您对用单引号“分隔”的内容感兴趣,因此,我将每个非
'
匹配并分组,将每个非'
都跟在单引号之后,然后依次跟上另一个单引号。 Thus, the char-class you want to match is simply [^']
... anything except '
. 因此,您要匹配的char-class只是
[^']
... ...,除了'
之外。
A possiple micro-optimization you could make to this suggested pattern would be to use a possessive quantifier ++
, which is similar to {1,}
. 您可以对这种建议的模式进行可能的微优化,就是使用所有格量词
++
,它类似于{1,}
。 Or, if you want to match an empty string if ''
is found, you could use *+
. 或者,如果要在找到
''
匹配空字符串,则可以使用*+
。 so 所以
if (preg_match_all("/'([^']++)'/", $subject, $matches))
var_dump($matches);
Should do the trick 应该做的把戏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.