in reply to special regexp

From what I see, the only thing wrong with your regular expression is, that you don't allow for any characters between the last match and the next match. From your data, this regular expression works :

F:\>perl -ne "while (m/\G.*?x(.)(?=x)/g) { print $1; }" axbxxcxdsxxxtx bcxt

An interesting boundary case is xxxx - what do you expect to be printed ? My solution prints x, as it matches xxx and is then left with x, which does not match. Conceivably, xx would also be a solution, as you could first match xxx and then move to the second x and match xxx again.

Replies are listed 'Best First'.
Re: Re: special regexp
by alfie (Pilgrim) on Mar 26, 2001 at 12:05 UTC
    Uhm, wasn't it you last time that told me about Death to Dot Star! last time? ;-)
    I had [^x]*? where you are now using .*? - so why is .*? working here but [^x]*? not? *totallypuzzled*
    --
    Alfie

      Yours is failing because you've anchored the match to where the previous one left off (with \G) and are also preventing moving across any 'x' characters that don't fit the x(.)(?=x) pattern by using the negative character class. The \G.*?x may traverse over an 'x' if the remainder of the expression fails.

      A simpler solution if you do not want to allow a "matched" 'x' to also count as a boundary 'x' is just:

      $_ = 'xaxbxcxdexfxxxx'; print $1 while /x(.)(?=x)/g; # abcfx

      If you do want to count 'matched' 'x' chars as potential boundary chars as well -- ie, 'xxxx' would produce 'xx' because there are two 'x' characters that have an 'x' on either side -- then:

      $_ = 'xaxbxcxdexfxxxx'; print $1 while /(?<=x)(.)(?=x)/g; # abcfxx