Regular Expression help please!

Regular Expression help please!

Post by Chris Doll » Sat, 24 Feb 2007 21:29:23



If you were to look at the description of a regular expression,
I think you'd be able to construct the answer yourself.

--
Chris "electric hedgehog" Dollin
"How am I to understand if you won't teach me?" - Trippa, /Falling/
 
 
 

Regular Expression help please!

Post by harryaj » Sat, 24 Feb 2007 21:29:24

I need a regular expression that checks the contents of a string as
follows -

The string must -

begin with the letters "CTN" but can be case insensitive

must be followed by at least one number possibly more

so

CTN1
CTN123
ctN1

will pass the check but

xCTN1
CTNk
cTn8d

won't!

can anyone help?

thanks

harry

 
 
 

Regular Expression help please!

Post by harryaj » Sat, 24 Feb 2007 21:45:13


I've tried several expressions but I just can't get the right one -
was hoping someone with a lot (I've only just started using them!) of
RE experience could just rattle one off?

thanks again

harry
 
 
 

Regular Expression help please!

Post by lester psi » Sat, 24 Feb 2007 21:49:05


have you tried:

Pattern p = Pattern.compile("^(C|c)(T|t)(N|n)\d+.*");
Matcher m = p.matcher("<yourstring>");
boolean b = m.matches();

if no furter characters are following the "CTN5<digits>.." string then
omit the '.*' clause at the end of the regular expression.
else, please look at the documentation of the javax.util.regex.Pattern
class in the j2se api.

lester
 
 
 

Regular Expression help please!

Post by harryaj » Sat, 24 Feb 2007 21:53:17


thanks for that Lester - made a note of giving RE stuff a good bash on
the weekend!
 
 
 

Regular Expression help please!

Post by Z » Sun, 25 Feb 2007 14:31:42


"^[c|C][t|T][n|N]\d+[\\s|$]"
 
 
 

Regular Expression help please!

Post by Z » Sun, 25 Feb 2007 14:39:55


Oops!

Make that: "^(c|C)(t|T)(n|N)\\d+(\\s|$)" or "^[cC][tT][nN]\\d+[\\s$]"
or maybe even "^[cC][tT][nN]\\d+\\b"
 
 
 

Regular Expression help please!

Post by Lew » Sun, 25 Feb 2007 14:55:41


Don't you mean "^[Cc][Tt][Nn]\d+$"? (Not written as a Java String literal.)

Java expression: String re = "^[Cc][Tt][Nn]\\d+$";

Is this homework?

Either way, you have to experiment with solutions you find here on Usenet. I
could be sadly mistaken, or Z could, or anyone could. OTOH, I did look up one
regex reference before posting this response.

< http://www.yqcomputer.com/ ;

- Lew