Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Regular Expression help please!

Thread view: 
harryajh - 23 Feb 2007 12:29 GMT
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
Chris Dollin - 23 Feb 2007 12:29 GMT
> 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.

Signature

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

harryajh - 23 Feb 2007 12:45 GMT
> > I need a regular expression that checks the contents of a string as
> > follows -
[quoted text clipped - 5 lines]
> Chris "electric hedgehog" Dollin
> "How am I to understand if you won't teach me?" - Trippa, /Falling/

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
lester psigal - 23 Feb 2007 12:49 GMT
> I need a regular expression that checks the contents of a string as
> follows -
[quoted text clipped - 24 lines]
>
> 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
harryajh - 23 Feb 2007 12:53 GMT
> > I need a regular expression that checks the contents of a string as
> > follows -
[quoted text clipped - 37 lines]
>
> lester

thanks for that Lester - made a note of giving RE stuff a good bash on
the weekend!
Z - 24 Feb 2007 05:31 GMT
> I need a regular expression that checks the contents of a string as
> follows -
[quoted text clipped - 10 lines]
>     cTn8d
> won't!

"^[c|C][t|T][n|N]\d+[\\s|$]"
Z - 24 Feb 2007 05:39 GMT
>> I need a regular expression that checks the contents of a string as
>> follows -
[quoted text clipped - 12 lines]
>
> "^[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"
Lew - 24 Feb 2007 05:55 GMT
harryajh wrote:
>> I need a regular expression that checks the contents of a string as
>> follows -
[quoted text clipped - 10 lines]
>>     cTn8d
>> won't!

> "^[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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.