How to use special variable in regex?

Discussion in 'Perl Misc' started by Peng Yu, Nov 27, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I want to use some variable in regex. But the below code does not work
    as I expected. Can somebody let me know what I am wrong?

    Thanks,
    Peng

    #!/usr/bin/perl

    use warnings;
    use strict;

    my @array = ('a' , 'b');
    my @strings = ('aa', 'ab', 'ba', 'bb', 'cc', 'cd');

    foreach (@array) {
    print "$_\n";
    my @subset = grep(/^$_.+$/, @strings);
    # The above line does not work as I want
    # I want search for all the stings in @strings
    # that are started with a letter in @array.
    foreach (@subset) {
    print "$_\n";
    }
    }

    print "here\n";

    my @subset = grep(/^a.+$/, @strings);
    foreach (@subset) {
    print "$_\n";
    }
    Peng Yu, Nov 27, 2008
    #1
    1. Advertising

  2. Peng Yu

    Todd Guest

    For the 2 sentences below:

    print "$_\n"; # -- #1
    my @subset = grep(/^$_.+$/, @strings); # -- #2

    the $_ inside grep is an alias to the current processing element in
    @string. So @subset will always be empty.

    Check 2 examples below and check the difference.

    #! /bin/perl -lw

    $_ = "a";
    print scalar grep (/^$_.+/, (a1, a2, a3));

    __END__

    0


    #! /bin/perl -lw

    $v = "a";
    print scalar grep (/^$v.+/, (a1, a2, a3));

    __END__

    3



    -Todd
    Todd, Nov 27, 2008
    #2
    1. Advertising

  3. Peng Yu wrote:
    >
    > I want to use some variable in regex. But the below code does not work
    > as I expected. Can somebody let me know what I am wrong?
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > my @array = ('a' , 'b');
    > my @strings = ('aa', 'ab', 'ba', 'bb', 'cc', 'cd');
    >
    > foreach (@array) {
    > print "$_\n";
    > my @subset = grep(/^$_.+$/, @strings);


    That is short for:

    my @subset = grep( $_ =~ /^$_.+$/, @strings );

    So you are trying to match the current element of @strings against the
    current element of @strings plus one character. And since the current
    element of @strings cannot have more characters than the current element
    of @strings it will never match.

    What you want is:

    foreach my $element ( @array ) {
    print "$element\n";
    my @subset = grep /^$element.+$/, @strings;



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Nov 27, 2008
    #3
  4. Peng Yu

    Bart Lateur Guest

    Peng Yu wrote:

    >I want to use some variable in regex. But the below code does not work
    >as I expected. Can somebody let me know what I am wrong?


    >#!/usr/bin/perl


    >my @array = ('a' , 'b');
    >my @strings = ('aa', 'ab', 'ba', 'bb', 'cc', 'cd');
    >
    >foreach (@array) {
    > print "$_\n";
    > my @subset = grep(/^$_.+$/, @strings);


    You're using $_ for two purposes, as your ordinary loop variable, and as
    the variable in grep. You're hoping to use a different variable, but of
    course, it'll use the same value twice: as the variable for grep.

    The fix is to use a different loop variable:

    foreach my $r (@array) {
    print "$r\n";
    my @subset = grep(/^$r.+$/, @strings);
    ...

    Although you could use a copy of the loop variable:

    foreach (@array) {
    print "$_\n";
    my $r = $_;
    my @subset = grep(/^$r.+$/, @strings);


    --
    Bart.
    Bart Lateur, Nov 27, 2008
    #4
  5. Peng Yu

    Dr.Ruud Guest

    Peng Yu schreef:

    > my @subset = grep(/^$_.+$/, @strings);


    Since you are not capturing, /^$_./ should do.

    Or use length()>1 and index()==0.
    Or "substr($string, 0, 1) eq $_" in stead of index()==0.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 27, 2008
    #5
  6. On 2008-11-27 06:53, John W. Krahn <> wrote:
    > That is short for:
    >
    > my @subset = grep( $_ =~ /^$_.+$/, @strings );
    >
    > So you are trying to match the current element of @strings against the
    > current element of @strings plus one character. And since the current
    > element of @strings cannot have more characters than the current element
    > of @strings it will never match.



    my @strings = ('a*');

    my @subset = grep( $_ =~ /^$_.+$/, @strings );
    foreach (@subset) {
    print "$_\n";
    }

    SCNR,
    hp
    Peter J. Holzer, Nov 28, 2008
    #6
  7. Peng Yu

    Guest

    On Fri, 28 Nov 2008 17:02:58 +0100, "Peter J. Holzer" <> wrote:

    >On 2008-11-27 06:53, John W. Krahn <> wrote:
    >> That is short for:
    >>
    >> my @subset = grep( $_ =~ /^$_.+$/, @strings );
    >>
    >> So you are trying to match the current element of @strings against the
    >> current element of @strings plus one character. And since the current
    >> element of @strings cannot have more characters than the current element
    >> of @strings it will never match.

    >
    >
    >my @strings = ('a*');
    >
    >my @subset = grep( $_ =~ /^$_.+$/, @strings );
    >foreach (@subset) {
    > print "$_\n";
    >}
    >
    > SCNR,
    > hp


    Yes, clever.
    ---------------------------
    use strict;
    use warnings;

    my $elem0 = 'a*';
    my @strings = ($elem0);

    my @subset = grep( s/^($_)(.+)$/$1-$2/, @strings );
    foreach (@subset) {
    print "$_\n";
    }
    my $evalstr =
    "
    print \"\\\$elem0 = \$elem0\\n\";
    if (\$elem0 =~ s/^($elem0)(.+)\$/\$1-\$2/)
    {
    print \"\\\$elem0 = \$elem0, \\\$1 = \$1, \\\$2 = \$2\\n\";
    }";

    print "\n$evalstr\n";
    eval $evalstr;

    __END__

    a-*

    print "\$elem0 = $elem0\n";
    if ($elem0 =~ s/^(a*)(.+)$/$1-$2/)
    {
    print "\$elem0 = $elem0, \$1 = $1, \$2 = $2\n";
    }
    $elem0 = a*
    $elem0 = a-*, $1 = a, $2 = *
    , Nov 28, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert
    Replies:
    1
    Views:
    308
    Martin v. Löwis
    Apr 16, 2008
  2. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  3. Astan Chee
    Replies:
    2
    Views:
    967
    John Machin
    Jul 21, 2009
  4. Gabriel Genellina

    Re: ignore special characters in python regex

    Gabriel Genellina, Jul 21, 2009, in forum: Python
    Replies:
    0
    Views:
    487
    Gabriel Genellina
    Jul 21, 2009
  5. Robert
    Replies:
    3
    Views:
    127
    Ben Bullock
    Apr 17, 2008
Loading...

Share This Page