in reply to special regexp

I'm a missing something? It seems to me this is a perfect target for split. You only have (if you are sure you always begin and end with 'x') to check for empty strings. Or join rightaway.

$str='xjxexrxoxexnexsx'; print join '', split /x/, $str; #prints my monkname

Cheers,

Jeroen
"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: special regexp
by jorg (Friar) on Mar 26, 2001 at 13:32 UTC
    The tricky bit u and nysus seem to have missed is that he would need the 'x' when he matches 'xjxoxrxgxXx' (uppercase X for clarity) and extracts 'jorgx' rather than just 'jorg'

    Jorg

    "Do or do not, there is no try" -- Yoda
      Aha!

      Let's keep trying it regex-less.

      If you need just any second character, you could use substr:

      my $str='xjxexrxoxexnxexsxxx'; my $idx = -1; my $str_2nd = ''; $str_2nd .= substr( $str, $idx+=2, 1) while( $idx < length $str); print "\n$str_2nd\n";
      You can achieve the same with split:
      my $str='xjxexrxoxexnxexsxxx'; my @chars = split //, $str; my @chars_2nd; while ( scalar @chars ){ shift @chars; push @chars_2nd, shift @chars; } print join '', @chars_2nd;

      Is this more like it? Just note that these solutions don't know what each other character ('x') is. It gets trickier if you allow more than one character between each delimiter 'x'.

      Jeroen
      "We are not alone"(FZ)

        Your first solution breaks with the string 'xnox' (which should not produce any output as there are no characters residing between two x'es), it just displays 'nx' which is undesired. Your second solution suffers from the same problem.
        Whenever there are alternating x's and other chars (even x's as well) your solution is perfect, however it does not take into account that their can be multiple chars between x's. (mind you the original post did not mention this either but it's not unrealistic to assume this)

        Jorg

        "Do or do not, there is no try" -- Yoda
      Here's a two step reg. expression that takes care of that problem:
      $line = "xaxlxxxfxixex"; $line =~ s/(x(\w)x)/$2/g; $line =~ s/x$//;