[英]Regex get all matches in one match
I'm searching for a regex that matches all words, but will give only one match. 我正在寻找一个匹配所有单词的正则表达式,但只会给出一个匹配项。
What I have: 我有的:
Regex: (.*\\={1})\\d+(\\D*)
正则表达式: (.*\\={1})\\d+(\\D*)
Input: max. Money (EU)=600000 Euro
输入: max. Money (EU)=600000 Euro
max. Money (EU)=600000 Euro
Output: Euro
输出: Euro
This works fine, but it's possible that the Input is like this: max. Money (EU)=600000 Euro plus 20000 for one person
这可以正常工作,但输入可能是这样的: max. Money (EU)=600000 Euro plus 20000 for one person
max. Money (EU)=600000 Euro plus 20000 for one person
. max. Money (EU)=600000 Euro plus 20000 for one person
。
Before the =
could be anything, the only thing that I know is that the =
is fixed. 在=
可以是任何东西之前,我唯一知道的是=
是固定的。 So every input have this. 所以每个输入都有这个。
So I'm searching for a regex that will give me for this input this output: Euro plus for one person
. 所以我正在寻找一个正则表达式,它将为我提供以下输出: Euro plus for one person
。
I tried to use a regex like (\\D+)
, but I'll get for this input 3 matches. 我试图使用像(\\D+)
这样的正则表达式,但是我将为此输入获得3个匹配项。 Does someone know how to get all occurences in one match? 有人知道如何在一场比赛中使所有事件发生吗?
One cannot match discontinuous text with 1 match operation. 一个匹配操作不能匹配不连续的文本。
The easiest workaround is to capture the whole substring after =
+ number, and then remove numbers from the match with preg_replace('~\\s*\\d+~', '', $m[1])
. 最简单的解决方法是在=
+ number之后捕获整个子字符串,然后使用preg_replace('~\\s*\\d+~', '', $m[1])
从匹配项中删除数字。
$re = '/=\d+\s*(.*)/';
$str = 'max. Money (EU)=600000 Euro plus 20000 for one person';
$res = '';
if (preg_match($re, $str, $m)) {
$res = preg_replace('~\s*\d+~', '', $m[1]);
}
echo $res; // => Euro plus for one person
Since you mention that a =
does not have to be followed by 1+ digits, you may really just explode
the string at the first =
and then remove digits in the second item: 由于您提到a =
不必跟着1+数字,因此您实际上可能只是将第一个=
处的字符串explode
,然后删除第二项中的数字:
$chunks = explode("=", $str, 2);
if (count($chunks) == 2) {
$res = preg_replace('~\s*\d+~', '', $chunks[1]);
}
See this PHP demo . 请参阅此PHP演示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.