[英]Match upto x regex or y regex
I currently have this: 我目前有这个:
^(.+)\(\w+\)|^(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\)
The #1 it only matches Foo's #1它只匹配Foo的
#2 Foo has which is correct #2 Foo有正确的
#3 does match foo but it's in the 3rd array item [2]: #3与foo匹配,但它在第3个数组项[2]中:
3rd array output:
(
[0] => Foo (100:200 - 300:400)
[1] =>
[2] => Foo
)
The bold is what I'm trying to match: 大胆是我想要匹配的:
Foo's (match11) this (100:200 - 300:400) the end #1 Foo(match11)这个 (100:200 - 300:400)结束#1
Foo has (not_matched) (100:200 - 300:400) the end #2 Foo有 (not_matched)(100:200 - 300:400)结束#2
Foo (100:200 - 300:400) the end #3 Foo (100:200 - 300:400)结束#3
note: im not trying to match the #1,#2,#3 at the end of each line, thats just for reference. 注意:我没有尝试匹配每行末尾的#1,#2,#3,仅供参考。
If "(100:200 - 300:400)" is found then get any text in front of it, elseif "(not_matched) (100:200 - 300:400)" found then get any text in front of it, else get text in front of "(100:200 - 300:400)" 如果找到“(100:200 - 300:400)”然后得到它前面的任何文字,如果“(not_matched)(100:200 - 300:400)”找到然后得到它前面的任何文字,否则得到“(100:200 - 300:400)”前面的文字
The elseif part "(not_matched) (100:200 - 300:400)" can be identified as it only has 1 white space between the 2 round brackets of not_matched and (100:200 - 300:400) 可以识别elseif部分“(not_matched)(100:200 - 300:400)”,因为它在not_matched和(100:200 - 300:400)的2个圆括号之间只有1个空格
Edit: 编辑:
This is what i'v come up with which does seem to work, though it requires some workarounds in php to be useful. 这就是我提出的似乎有效的方法,虽然它需要一些PHP中的解决方法才有用。
(.+)\s\(\w+\)\s\(|(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\)
Working example: http://www.rubular.com/r/NSpGcnyg0p 工作示例: http : //www.rubular.com/r/NSpGcnyg0p
For some reason it doesn't seem to save my example, so you will have to copy/paste it in. 出于某种原因,它似乎没有保存我的示例,因此您必须将其复制/粘贴。
But the regex doesn't have a direct match on each of them, thats why I have to remove the empty array element in php so that I get the result in the [1] element. 但正则表达式并没有直接匹配它们,这就是为什么我必须删除php中的空数组元素,以便我在[1]元素中得到结果。
Can anyone see what I'm doing wrong in my regex? 任何人都可以在我的正则表达式中看到我做错了什么?
Try this: 尝试这个:
^.*?(?=\s*(?:\(not_matched\)\s)?\(\d+:\d+\s*-\s*\d+:\d+\))
or, in PHP: 或者,在PHP中:
if (preg_match(
'/^ # Anchor the match at start of line
.*? # Match any number of characters lazily
(?= # until just before the following:
\s* # - optional whitespace
(?: # - the following group:
\(not_matched\)\s # - literal (not_matched), followed by 1 whitespace
)? # (which is optional)
\(\d+:\d+\s*-\s*\d+:\d+\) # a group like (nnn:mmm - ooo:ppp)
) # End of lookahead assertion
/x',
$subject, $regs)) {
$result = $regs[0];
} else {
$result = "";
}
This will match your second example: (.+)(\\([\\D]+\\).+)(\\#\\d+)
. 这将匹配您的第二个示例:
(.+)(\\([\\D]+\\).+)(\\#\\d+)
。
And this one will match two others: (.+)(\\([\\d\\W]+\\).+)(\\#\\d+)
. 这个将匹配另外两个:
(.+)(\\([\\d\\W]+\\).+)(\\#\\d+)
。
如果我清楚地明白,这应该是诀窍:
/^(.+)(?:\(not_matched\)\s)?(?:\(\d+:\d+\s-\s\d+:\d+\))\s.+\s(#\d+)$/i
This does seem to work, but needs a little workaround to be useful in php. 这似乎工作,但需要一些解决方法在PHP中有用。 (read my original question).
(阅读我原来的问题)。
I'll select this as the answer if no one else has any ideas... 如果其他人没有任何想法,我会选择这个作为答案......
(.+)\s\(\w+\)\s\(|(.+)\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\)
The following pattern will match everything, the result is stored into the wanted
key : 以下模式将匹配所有内容,结果存储在
wanted
密钥中:
$PATTERN = '/
(?P<wanted>.*?)\s* # everything
(\(.+\s.+\)\s+)? # maybe followed by
(?= # that ends with
\(\d{3}:\d{3}\s-\s\d{3}:\d{3}\)
)
/x';
preg_match($PATTERN, "Foo's (match11) this (100:200 - 300:400) the end", $matches);
var_dump($matches['wanted']);
preg_match($PATTERN, "Foo has (not matched) (100:200 - 300:400) the end", $matches);
var_dump($matches['wanted']);
preg_match($PATTERN, "Foo (100:200 - 300:400) the end", $matches);
var_dump($matches['wanted']);
Tripple checked, in multiple Versions. Tripple已检查过多个版本。
<?php
$string[] = "Foo's (match11) this (100:200 - 300:400) ";
$string[] = "Foo has (not_matched) (100:200 - 300:400) ";
$string[] = "Foo (100:200 - 300:400) ";
$reg = "~(.*)(\([^\)]*\))?\s\(\d{3}\:\d{3}\s\-\s\d{3}\:\d{3}\)~iU";
foreach ( $string as $s ){
preg_match_all ( $reg, $s, $m , PREG_SET_ORDER);
print "<br />String: ". $s . "<br /><pre>";
print_r ( $m );
print "</pre><br />OR";
print "The String Required is: " . $m[0][1] . "<br />";
}
?>
Its working, and you can get the needed string on 它的工作,你可以得到所需的字符串
$output = $m[0][1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.