Syntactic Confectionery Delight | |
PerlMonks |
Pattern matching: Why no \1 in [ ]?by Not_a_Number (Prior) |
on Aug 07, 2003 at 19:37 UTC ( [id://282006]=perlquestion: print w/replies, xml ) | Need Help?? |
Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question: I thought, naively, that the easiest way of matching a string such as 'XOX' or 'TNT' but not 'XXX' or 'TTT' would be: /(.)[^\1]\1/...but this doesn't work:
This prints match. Indeed, it seems to match if I replace the middle 'B' with any single character (including '1' and '\'). Even more strangely, to me, if I 'un-negate' the character class: /(.)[\1]\1/...I don't seem to match anything (BTW, I get no complaints with strictures and warnings). Context: I'm trying to code a simple substitution cipher solver (eg where 'ABCABC' will match 'murmur' and 'tsetse' but not 'booboo'). I'm aware that there are other ways of doing it, notably merlyn's post here. However, he uses (unless I'm mistaken) negative loook-behind magic, and I haven't got that far in Mastering Regular Expressions yet :-). I'm not looking for a solution, I'm just curious as to what /[^\1]/ and /[\1]/ actually match? I've scoured perlre and done an index search for the (admittedly large :-) part of Mastering Regular Expressions that I still haven't read, but found nothing that appears relevant to my question (that's not to say that there is nothing relevant, just that I might not have understood its relevance...). TIA for your enlightenment. dave
Back to
Seekers of Perl Wisdom
|
|