PHP Regexp (PCRE) - Find a set of all substrings

僤鯓⒐⒋嵵緔 提交于 2019-12-22 10:34:45

问题


I have the following string, for example:

aaXXccYYeeXX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ZZvv

How can I find all XX.*YY.*ZZ parts in the string? (possibly by using preg_match())

  • XX cc YY eeXX_ ZZ
  • XX _ZZkk YY mmXX_ ZZ
  • XX _ZZnnXXoo YY uuXX_ ZZ
  • XX oo YY uuXX_ ZZ

Plus all longer matches, as:

  • XX cc YY eeXX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ ZZ

回答1:


Thank everybody for help.

My solution based on 'bobbogo' solution. Thank you.

Regular expression:

(?=(XX.*?YY.*?ZZ))(?=(.*ZZ))

Result (from RegexBuggy):

1 XXccYYeeXX_ZZ     XXccYYeeXX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ZZ
2 XX_ZZkkYYmmXX_ZZ      XX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ZZ
3 XX_ZZnnXXooYYuuXX_ZZ  XX_ZZnnXXooYYuuXX_ZZ
4 XXooYYuuXX_ZZ     XXooYYuuXX_ZZ

Possible it can by more optimized? I am not big professional in regex.




回答2:


This seems quite straight forward at first glance (apologies if I am missing something). For the first part, use repeated matches of:

XX(.*?)YY(.*?)ZZ

For the longer match I suggest a separate match of:

XX(.*?)YY(.*)ZZ

EDIT

Aha! the patterns overlap (thanks folks). In this case, you will have to loop. Probably easiest to strip off any leading .*?XX before re-trying the match. In perl land you would use

while (/XX(.*?)YY(.*?)ZZ/) {
    print "[$1] [$2]\n";
    s/^.*?XX//;
}

If you are desperate for a single regex, then m//g in array context will come to your rescue. (Not sure what the corresponding php might look like.)

@a = /XX(?=(.*?YY.*?ZZ))/g;

The array @a will contain the overlapping strings, but with the initial XX stripped off.



来源:https://stackoverflow.com/questions/5231482/php-regexp-pcre-find-a-set-of-all-substrings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!