Regular Expression help please!

C

Chris Dollin

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

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

harryajh

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
 
H

harryajh

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

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
 
L

lester psigal

harryajh said:
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

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
 
H

harryajh

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

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

Z

harryajh said:
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!

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

Z

Z said:
harryajh said:
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!

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

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"
 
L

Lew

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

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://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html>

- Lew
 

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 please 8
Malicious Coding Help Please 5
Hello and Help please :-) 1
Please help me!!! 3
Can't solve problems! please Help 0
Please help 2
Please help 7
Help with my navigation, please 0

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top