in reply to matching patterns

I got it just need to add $ at the end of the line :) if ( $value =~ m/^((A|N)+|((A|N)*(NP)?)(A|N)*)N$/ ) { print "match: $value\n"; }

Replies are listed 'Best First'.
Re^2: matching patterns
by throop (Chaplain) on Jun 08, 2008 at 22:38 UTC
    Gentle Sovixi,

    Your pattern matches; good. May I offer some tips?

    1. Use the <code> tags to make your code easier to read. (I see you've updated it to do so; good.)
    2. Your pattern now would match the 1-char string 'N'. Is that really what you want?
    3. You're using capturing parenthesis, but not using the value.
    4. When matching single letters, the square brackets read more easily that the '|' in parens.
    5. Use the 'x' modifier to make your code more readable and commentable. Viz
    if ( $value =~ m/^(?: [AN]+ # Either at least one A|N |[AN]* # or an A|N run (?: NP)? # Possibly interrupted by NP [AN]* ) N$/x ){ # With a terminal N. print "match: $value\n"; }
    Something still looks suspicious to me. Your first pattern match is a choice between [AN]+ and [AN]*(NP)?[AN]*. That second choice can match the null string. Seems odd.

    throop