Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular Expression help please!

Reply
Thread Tools

Regular Expression help please!

 
 
Chris Dollin
Guest
Posts: n/a
 
      02-23-2007
harryajh wrote:

> 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.

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

 
Reply With Quote
 
 
 
 
harryajh
Guest
Posts: n/a
 
      02-23-2007
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

 
Reply With Quote
 
 
 
 
harryajh
Guest
Posts: n/a
 
      02-23-2007
On 23 Feb, 12:29, Chris Dollin <chris.dol...@hp.com> wrote:
> harryajh wrote:
> > 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.
>
> --
> 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

 
Reply With Quote
 
lester psigal
Guest
Posts: n/a
 
      02-23-2007
harryajh wrote:
> 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
 
Reply With Quote
 
harryajh
Guest
Posts: n/a
 
      02-23-2007
On 23 Feb, 12:49, lester psigal <lesterpsi...@yahoo.de> wrote:
> harryajh wrote:
> > 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


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

 
Reply With Quote
 
Z
Guest
Posts: n/a
 
      02-24-2007
harryajh wrote:
> 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|$]"
 
Reply With Quote
 
Z
Guest
Posts: n/a
 
      02-24-2007
Z wrote:
> harryajh wrote:
>> 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"
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-24-2007
harryajh wrote:
>> 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!


Z wrote:
> "^[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
 
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
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57