Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with regexp

Reply
Thread Tools

Help with regexp

 
 
mike
Guest
Posts: n/a
 
      05-11-2009
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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      05-11-2009
On Sun, 10 May 2009 23:47:23 -0700 (PDT), mike <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      05-11-2009
On Mon, 11 May 2009 00:04:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>On Sun, 10 May 2009 23:47:23 -0700 (PDT), mike <(E-Mail Removed)> 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
 
Reply With Quote
 
Frank Seitz
Guest
Posts: n/a
 
      05-11-2009
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 fr Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      05-11-2009
On May 11, 2:47*am, mike <(E-Mail Removed)> 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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-11-2009
ccc31807 <(E-Mail Removed)> wrote in news:b2ab8de9-e977-4610-985a-
(E-Mail Removed):

> On May 11, 2:47*am, mike <(E-Mail Removed)> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments