[英]Regex capture group 2 to capture what does not match capture group 1
I am wanting to capture Owner or Builder in group 1, otherwise any text up until hyphen and/or space followed by a digit is encountered in group 2, then any digit til end of line in group 3 from the following text:- 我想捕获组1中的所有者或生成器,否则,在组2中遇到直到连字符和/或空格后接数字的所有文本,然后从以下文本到组3中行尾的任何数字:
PRD Hervey Bay 07 2525 1919
RG Sprake & Co Maryborough 4141 2424
Owner Robert - 0429 966 391
Owner Maureen - 07 8888 4444 - 0422000 111
Owner Wayne - 0430 555 666
Builder Scott - 0444 555 777
The expression I am currently using:- 我当前正在使用的表达式:-
/(Owner|Builder)([^-]+)-|\s+(\d.*)/gm
Does not match 'PRD Hervey Bay' and 'RG Sprake & Co Maryborough' in group 2. 与组2中的“ PRD Hervey Bay”和“ RG Sprake&Co Maryborough”不匹配。
Any help appreciated! 任何帮助表示赞赏!
Trent. 特伦特。
You can use this regex to get the results you want: 您可以使用此正则表达式获取所需的结果:
/^(Owner |Builder )?(.*?)(?=-| \d)[- ]+(.*)$/m
It looks for (optionally) the string Owner
or Builder
; 它查找(可选)字符串Owner
或Builder
; followed by some minimal number of characters until a -
or a space followed by a digit; 后面跟一些最少的字符,直到-
或空格后跟一个数字; followed by some number of -
or space, then characters until end of line. 后面跟一些-
或空格,然后是字符,直到行尾。 To use in PHP: 在PHP中使用:
$text = <<<EOD
PRD Hervey Bay 07 2525 1919
RG Sprake & Co Maryborough 4141 2424
Owner Robert - 0429 966 391
Owner Maureen - 07 8888 4444 - 0422000 111
Owner Wayne - 0430 555 666
Builder Scott - 0444 555 777
EOD;
preg_match_all('/^(Owner |Builder )?(.*?)(?=-| \d)[- ]+(.*)$/m', $text, $matches);
print_r($matches);
Output: 输出:
Array (
[0] => Array (
[0] => PRD Hervey Bay 07 2525 1919
[1] => RG Sprake & Co Maryborough 4141 2424
[2] => Owner Robert - 0429 966 391
[3] => Owner Maureen - 07 8888 4444 - 0422000 111
[4] => Owner Wayne - 0430 555 666
[5] => Builder Scott - 0444 555 777
)
[1] => Array (
[0] =>
[1] =>
[2] => Owner
[3] => Owner
[4] => Owner
[5] => Builder
)
[2] => Array (
[0] => PRD Hervey Bay
[1] => RG Sprake & Co Maryborough
[2] => Robert
[3] => Maureen
[4] => Wayne
[5] => Scott
)
[3] => Array (
[0] => 07 2525 1919
[1] => 4141 2424
[2] => 0429 966 391
[3] => 07 8888 4444 - 0422000 111
[4] => 0430 555 666
[5] => 0444 555 777
)
)
Note that this does leave some spaces and -
in the last group, you can most easily remove them (if desired) by using array_walk
: 请注意,这确实留有一些空间,并且-
在最后一组中,您可以使用array_walk
轻松删除它们(如果需要):
array_walk($matches[3], function (&$v) { $v = preg_replace('/[^\d]/', '', $v); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.