Capturing a pattern of unknown repitition in PCRE

前端 未结 2 1149
猫巷女王i
猫巷女王i 2020-12-11 23:52

This may be a quick question for experienced regular expressionists, but I\'m having trouble getting my match to execute correctly.

Suppose I had a string that looke

相关标签:
2条回答
  • 2020-12-12 00:27

    No, it is not possible to match an unknown number of capture groups.

    If you try to repeat a capture group, it will always contain the last value captured.

    Could you explain a bit more broadly what you're trying to do? Perhaps there is another simple way to do it (possibly without regular expressions).

    0 讨论(0)
  • 2020-12-12 00:47

    If you want the items in the subdomain, and then all matches between the dashes... This should work:

    $string = "http://aaa-bbbb-cc-ffffffffd-eee-.sub.dom";
    
    preg_match("/^http:\/\/([\w-]+?)\..*$/i", $string, $match);
    
    $parts = explode('-', $match[1]);
    
    print_r($parts);
    

    Short of that you will probably have to build a small parsing script to parse the string yourself if that doesn't do it for you.

    0 讨论(0)
提交回复
热议问题