[pcre-dev] Forward back references

トップ ページ
このメッセージを削除
著者: Juergen Leising
日付:  
To: pcre-dev
題目: [pcre-dev] Forward back references

Hello,

I have difficulties with forward back references. For example:

    /^abc(d)\1$/ 
versus 
    /^abc\1(d)$/


Shouldn't abcdd match with both patterns?
With the first one (actual back reference) it does.
But not with the second one (forward back reference):

re> /^abc(d)\1$/D

------------------------------------------------------------------
  0  24 Bra
  3     ^
  4     abc
 10   7 CBra 1
 15     d
 17   7 Ket
 20     \1
 23     $
 24  24 Ket
 27     End
------------------------------------------------------------------
Capturing subpattern count = 1
Max back reference = 1
Options: anchored
No first char
No need char

data> abcdd

0: abcdd
1: d


re> /^abc\1(d)$/D

------------------------------------------------------------------
  0  24 Bra
  3     ^
  4     abc
 10     \1
 13   7 CBra 1
 18     d
 20   7 Ket
 23     $
 24  24 Ket
 27     End
------------------------------------------------------------------
Capturing subpattern count = 1
Max back reference = 1
Options: anchored
No first char
No need char

data> abcdd

No match




Ok, man pcrepattern says, named back references can be used in
both ways. But I don't get the results I would have expected
with those ones, either:

re> /^((?P=abc)|X)(?<abc>x|y)+/D

------------------------------------------------------------------
  0  35 Bra
  3     ^
  4   8 CBra 1
  9     \2
 12   5 Alt
 15     X
 17  13 Ket
 20   7 CBra 2
 25     x
 27   5 Alt
 30     y
 32  12 KetRmax
 35  35 Ket
 38     End
------------------------------------------------------------------
Capturing subpattern count = 2
Max back reference = 2
Named capturing subpatterns:
  abc   2
Options: anchored
No first char
No need char

data> Xx

0: Xx
1: X
2: x
data> Xy

0: Xy
1: X
2: y
data> xx

No match
data> xy

No match



Why don't the last ones match, as well?

With the pattern the other way round xx does match:

re> /(?<abc>x|y)((?P=abc)|X)/D

------------------------------------------------------------------
  0  34 Bra
  3   7 CBra 1
  8     x
 10   5 Alt
 13     y
 15  12 Ket
 18   8 CBra 2
 23     \1
 26   5 Alt
 29     X
 31  13 Ket
 34  34 Ket
 37     End
------------------------------------------------------------------
Capturing subpattern count = 2
Max back reference = 1
Named capturing subpatterns:
  abc   1
No options
No first char
No need char

data> xx

0: xx
1: x
2: x


I refer to pcre-7.4.

Best regards,

Juergen