Regular expression behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Briska

    Regular expression behavior

    Given the script:

    --------
    use strict;
    use warnings;

    my $text = 'blob:blob';

    doit($text);
    doit($text);
    doit($text);

    sub doit
    {
    my ($aa) = @_;

    if( $aa =~ m?[^:]+:.*? ) {
    print "Good!\n";
    }
    else {
    print "Bad! \n";
    }
    }

    ----

    This produces the output:

    Good!
    Bad!
    Bad!

    -----

    I think that I may have a mistake on the regular expression syntax,
    but why does this show one behavior once and another behavior for all
    subsequent calls?

    If I change the regex to m/[^:]+:.*/ it works fine.

    I was under the impression that I could use any delimiter around my
    regex, but I seem to be mistaken.

    -Adam
  • Gunnar Hjalmarsson

    #2
    Re: Regular expression behavior

    Adam Briska wrote:[color=blue]
    > Given the script:
    >
    > --------
    > use strict;
    > use warnings;
    >
    > my $text = 'blob:blob';
    >
    > doit($text);
    > doit($text);
    > doit($text);
    >
    > sub doit
    > {
    > my ($aa) = @_;
    >
    > if( $aa =~ m?[^:]+:.*? ) {
    > print "Good!\n";
    > }
    > else {
    > print "Bad! \n";
    > }
    > }
    >
    > ----
    >
    > This produces the output:
    >
    > Good!
    > Bad!
    > Bad!
    >
    > -----
    >
    > I think that I may have a mistake on the regular expression syntax,
    > but why does this show one behavior once and another behavior for all
    > subsequent calls?
    >
    > If I change the regex to m/[^:]+:.*/ it works fine.
    >
    > I was under the impression that I could use any delimiter around my
    > regex, but I seem to be mistaken.[/color]

    So it seems.

    Why didn't you check the docs for the m// operator before asking here?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    Working...