Help with regexp

M

mike

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
 
S

sln

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
 
S

sln

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
 
F

Frank Seitz

mike said:
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
 
C

ccc31807

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
 
A

A. Sinan Unur

# 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 <[email protected]>
(remove .invalid and reverse each component for email address)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

help with regexp 5
Issue with textbox script? 0
Help with my responsive home page 2
Help with Loop 0
Need help with this script 4
Help in this program. 2
Regexp help 7
Python battle game help 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top