Regular Expression help please!

Discussion in 'Java' started by Chris Dollin, Feb 23, 2007.

  1. Chris Dollin

    Chris Dollin Guest

    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/
    Chris Dollin, Feb 23, 2007
    #1
    1. Advertising

  2. Chris Dollin

    harryajh Guest

    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
    harryajh, Feb 23, 2007
    #2
    1. Advertising

  3. Chris Dollin

    harryajh Guest

    On 23 Feb, 12:29, Chris Dollin <> 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
    harryajh, Feb 23, 2007
    #3
  4. 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
    lester psigal, Feb 23, 2007
    #4
  5. Chris Dollin

    harryajh Guest

    On 23 Feb, 12:49, lester psigal <> 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!
    harryajh, Feb 23, 2007
    #5
  6. Chris Dollin

    Z Guest

    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|$]"
    Z, Feb 24, 2007
    #6
  7. Chris Dollin

    Z Guest

    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"
    Z, Feb 24, 2007
    #7
  8. Chris Dollin

    Lew Guest

    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
    Lew, Feb 24, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,289
  2. dotnetprogram

    Regular Expression help please...

    dotnetprogram, Dec 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    2,067
    Rajesh.V
    Dec 17, 2003
  3. Replies:
    3
    Views:
    342
  4. Replies:
    2
    Views:
    384
    =?Utf-8?B?R3VmZmE=?=
    Apr 21, 2006
  5. KK
    Replies:
    2
    Views:
    552
    Big Brian
    Oct 14, 2003
Loading...

Share This Page