1

I gave up. I am using PHP to match capture all [A-Za-z0-9-.]{1,7} inside EITHER parenthesis or square brackets.

I have the following reg expresion

$pattern = /(?:(?:\(|\[)([A-Za-z0-9-.]{1,7})(?:\)|\]))/

I cannot understand, why

$r->filename = Flood Control Geo Survey (Bali) (Indo) (2005) (V1.0) [acc].doc

if (preg_match($pattern, $r->filename, $matches)) {
    echo $r->filename, '<br />';
    echo '<pre>', var_dump($matches), '</pre><hr />';

}

Outputs:

array(2) {
  [0]=>
  string(6) "(Bali)"
  [1]=>
  string(4) "Bali"
}
  1. I cannot understand why [0] captured the opening parenthesis and closing parenthesis when my capture group is AFTER its declaration. [1] is correct and what I need, why i still have [0]?

  2. Why is it stopping?, there's still (Indo), (2005), (V1.0) and [acc]? Why are those not captured?

  3. My desired output is:

    array() { [0]=> string(6) "Bali" [1]=> string(4) "Indo" [2]=> string(4) "2005" [3]=> string(4) "V1.0" [4]=> string(4) "acc" }

Can somebody please help? Thank you!

arvil
  • 840
  • 11
  • 27
  • 4
    Group 0 always contains the whole match, the capturing groups start with 1. You might want to use [preg_match_all](http://php.net/manual/en/function.preg-match-all.php). – Sebastian Proske Nov 08 '16 at 15:54
  • 2
    [Read the manual](http://php.net/manual/en/function.preg-match.php): _"$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."_ – M. Eriksson Nov 08 '16 at 15:55
  • @SebastianProske i saw it, `[0]` array contains `(,[,),]` and `[1]` array contains the stripped one? I do not understand, is it built in that php function or that's how I built my pattern? – arvil Nov 08 '16 at 15:56
  • See @SebastianProske comment and use `preg_match_all` then use the first index, https://eval.in/674300. – chris85 Nov 08 '16 at 15:57
  • 1
    That's how patterns are matched in pretty much every regex implementation I know. – Sebastian Proske Nov 08 '16 at 15:57
  • thank you very much guys @SebastianProske if you will reply with `preg_match_all` I will label mark it as answer. Thank you. – arvil Nov 08 '16 at 15:58
  • @SebastianProske: it isn't always the case, for example in Python: `re.findall(r'abc(...)def', s)` returns only the list of capture groups, without the whole match. – Casimir et Hippolyte Nov 08 '16 at 16:07
  • @CasimiretHippolyte But all the other re methods that are used to match return a match object, which again stores the whole match in group 0. – Sebastian Proske Nov 08 '16 at 16:18

0 Answers0