Help with regexp

Discussion in 'Perl Misc' started by mike, May 11, 2009.

  1. mike

    mike Guest

    Hi,

    I have the following string that I need to check the following:

    CXP_1212232_R1A01

    It starts with CXP and has seven digits after first underscore and
    that after second underscore it begins with an cap R followed by a
    cap letter and a digit. Then the last digits in the string can be any
    number of digits ( it is like an index).

    I have ttried the following regexp:

    if($name !~ m/^[CXP]_\d{7}_[R]{1}\d{1}[A-Z]{1}\d+/){
    print "String should be something like, CXP_1233445_R1A01\n";
    print "You had the following, $name\n";
    exit 1;
    }

    I tried the following,CXP_1212232_R1A01, but I seem to go into the if
    statement. However this should be an acceptable name. Any hints?

    br,

    //mike
     
    mike, May 11, 2009
    #1
    1. Advertising

  2. mike

    Guest

    On Sun, 10 May 2009 23:47:23 -0700 (PDT), mike <> wrote:

    >It starts with CXP and has seven digits after first underscore and
    >that after second underscore it begins with an cap R followed by a
    >cap letter and a digit. Then the last digits in the string can be any
    >number of digits ( it is like an index).
    >

    /^CXP.*?\d{7}.*?_R[A-Z]\d\d*?$/

    -sln
     
    , May 11, 2009
    #2
    1. Advertising

  3. mike

    Guest

    On Mon, 11 May 2009 00:04:17 -0700, wrote:

    >On Sun, 10 May 2009 23:47:23 -0700 (PDT), mike <> wrote:
    >
    >>It starts with CXP and has seven digits after first underscore and
    >>that after second underscore it begins with an cap R followed by a
    >>cap letter and a digit. Then the last digits in the string can be any
    >>number of digits ( it is like an index).
    >>

    >/^CXP.*?\d{7}.*?_R[A-Z]\d\d*?$/
    >


    /^CXP.*?_\d{7}.*?_R[A-Z]\d\d*?$/

    -sln
     
    , May 11, 2009
    #3
  4. mike

    Frank Seitz Guest

    mike wrote:
    >
    > CXP_1212232_R1A01

    [...]
    > I have ttried the following regexp:
    >
    > if($name !~ m/^[CXP]_\d{7}_[R]{1}\d{1}[A-Z]{1}\d+/){


    The square brackets around "CXP" and "R" are wrong ([...] means
    character class) and the "{1}" are useless, the rest is ok.

    /^CXP_\d{7}_R\d[A-Z]\d+/

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
     
    Frank Seitz, May 11, 2009
    #4
  5. mike

    ccc31807 Guest

    On May 11, 2:47 am, mike <> wrote:
    > I have the following string that I need to check the following:
    >
    > CXP_1212232_R1A01


    # 1st, assign your text to a variable
    my $var = 'CXP_1212232_R1A01'; #or somesuch
    # 2nd, assign the parts to different variables
    my ($first, $second, $third) = split /_/, $var;
    # 3rd, test it
    if ($first =~ /CXP/) { ... do stuff ... }
    if ($second =~ /[0-]{7}/) { ... do stuff ... }
    if ($third =~ /R\d[0-9A-Z]+/) { ... do stuff ... )

    If getting the key in $third is necessary, use substr like this:

    my $key = substr($third, 2);

    CC
     
    ccc31807, May 11, 2009
    #5
  6. ccc31807 <> wrote in news:b2ab8de9-e977-4610-985a-
    :

    > On May 11, 2:47 am, mike <> wrote:
    >> I have the following string that I need to check the following:
    >>
    >> CXP_1212232_R1A01

    >
    > # 1st, assign your text to a variable
    > my $var = 'CXP_1212232_R1A01'; #or somesuch
    > # 2nd, assign the parts to different variables
    > my ($first, $second, $third) = split /_/, $var;
    > # 3rd, test it


    Careless as usual:

    > if ($first =~ /CXP/) { ... do stuff ... }


    Is

    +++CXP!!!_1212232_R1A01

    acceptable?

    > if ($second =~ /[0-]{7}/) { ... do stuff ... }


    This specifies that the second part of the string should match a string
    of seven zeros or dashes. I'll ask again. Is

    CXP_$++-0-0-00_R1A01

    acceptable?

    > if ($third =~ /R\d[0-9A-Z]+/) { ... do stuff ... )


    So I guess you think:

    CXP_1212232_ZR\x{1815}AAAAAAAAAAAAAAAAAAAAAAAAAAA

    is also OK.

    To the OP, see script below which I wrote based on my understanding of
    your problem:

    #!/usr/bin/perl

    use strict;
    use warnings;

    while ( <DATA> ) {
    next unless /\S/;
    chomp;
    /\ACXP_[0-9]{7}_R[0-9][A-Z][0-9]{2}\z/ and next;
    print "'$_' did not match\n";
    }

    __DATA__
    CXP_1212232_R1A01
    CXP_1212232_RAB01




    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, May 11, 2009
    #6
    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. Greg Hurrell
    Replies:
    4
    Views:
    166
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    506
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    377
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    191
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page