in reply to Re: special regexp
in thread special regexp

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

Replies are listed 'Best First'.
Re: Re: Re: special regexp
by jeroenes (Priest) on Mar 26, 2001 at 13:49 UTC
    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
        Well, I already mentioned this in my post (last line). There is no easy way to solve this, as I said. How would one now if 'xtxaxix' would mean taxi or tai?

        Jeroen
        "We are not alone"(FZ)

Re: Re: Re: special regexp
by nysus (Parson) on Mar 26, 2001 at 13:52 UTC
    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$//;