Front page | perl.beginners |
Postings from April 2012
Re: regex
Thread Previous
|
Thread Next
From:
Somu
Date:
April 9, 2012 13:09
Subject:
Re: regex
Message ID:
CAL5VgOmvgTN7vvcH-ts_U1Y8bzG1n68rgF9A7MuH5n03GJVUzg@mail.gmail.com
WOW!!! Still to see the world... Thanks :)
On Mon, Apr 9, 2012 at 4:04 AM, John W. Krahn <jwkrahn@shaw.ca> wrote:
> Somu wrote:
>
>> Hello everyone...
>>
>
> Hello,
>
>
> Thanks for the previous replies. I'm really improving!
>> A new problem..
>>
>> Check if the word begins with un or in and has exactly 5 letters.
>> $word =~ '^(un|in).{3}$'
>>
>
> You should use the matching operator on the right hand side of the binding
> operator instead of a string, so either:
>
> $word =~ m'^(un|in).{3}$';
>
> If you don't want interpolation, or:
>
> $word =~ /^(un|in).{3}$/;
>
> You are using capturing parentheses, which may, or may not be required,
> but you would normally use non-capturing parentheses:
>
> $word =~ /^(?:un|in).{3}$/;
>
> Or because there is only one letter difference it would be better to use a
> character class:
>
> $word =~ /^[ui]n.{3}$/;
>
> Also, the use of . will match more than just letters. If you want the
> string to only contain letters then:
>
> $word =~ /^[ui]n[[:alpha:]]{3}$/;
>
> And finally, the use of the $ anchor implies that the string could end
> with an optional newline. If you want to ensure that there is no newline
> present then:
>
> $word =~ /\A[ui]n[[:alpha:]]{3}\z/;
>
>
>
> John
> -- *
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction. -- Albert Einstein*
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
Thread Previous
|
Thread Next