Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Regex: If not beginning of string match end

by theneil (Novice)
on Jan 17, 2013 at 16:52 UTC ( #1013817=perlquestion: print w/ replies, xml ) Need Help??
theneil has asked for the wisdom of the Perl Monks concerning the following question:

Having some trouble I want to match the last 2 characters of any string (if numeric) that doesn't look like this:
^[s][0|6]00[0-9]{2}
I've been trying neg/pos lookaheads but I can't seem to match it. Any suggestions? Thanks!

Comment on Regex: If not beginning of string match end
Download Code
Re: Regex: If not beginning of string match end
by 2teez (Priest) on Jan 17, 2013 at 17:24 UTC

    Really, I don't know how your data looks like or how you are getting it..
    However, does something like this work for you:

    use strict; use warnings; while(<DATA>){ chomp; print $_,$/ if /\d{2}$/; } __DATA__ PHY/99/3006 PHY/99/3009 PHY/99/2678 PHY/99/390O PHY/99/129R PHY/99/UY90
    Update
    Output:
    PHY/99/3006 PHY/99/3009 PHY/99/2678 PHY/99/UY90

    You can also check this Simple word matching

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Regex: If not beginning of string match end
by Kenosis (Priest) on Jan 17, 2013 at 20:19 UTC

    ...I want to match the last 2 characters of any string (if numeric) that doesn't look like this:...

    And here is your "this" explained that you don't want the last two characters to match:

    /^[s][0|6]00[0-9]{2}/ ^ Start of string Char class [s] matches one of the following chars: s Char class [0|6] matches one of the following chars: 0|6 00 Literal `00` Char class [0-9] 2 times matches one of the following chars: 0-9

    Your regex matches (or doesn't match) much more than the last two characters of any string. 2teez already mentioned your data, and it would be helpful if you would show a sample along with what you'd like to exclude from it via a regex.

Re: Regex: If not beginning of string match end
by CountZero (Bishop) on Jan 17, 2013 at 21:57 UTC
    use Modern::Perl; while (<DATA>) { chomp; next if /^[s][0|6]00[0-9]{2}/; next unless /(\d{2})$/; say "$_ -> $1"; } __DATA__ s00023 s50012 s600ad s00012 abc-def-123456
    The first test rejects all data that fit the ^[s][0|6]00[0-9]{2} pattern.

    The second test rejects all data that does not end in two digits.

    If the data "survives" both tests it gets printed.

    s50012 -> 12 abc-def-123456 -> 56

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013817]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-09-21 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls