Regular Expression help please!

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

  1. Chris Dollin

    Chris Dollin Guest

    If you were to look at the description of a regular expression,
    I think you'd be able to construct the answer yourself.
    Chris Dollin, Feb 23, 2007
    1. Advertisements

  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



    will pass the check but



    can anyone help?


    harryajh, Feb 23, 2007
    1. Advertisements

  3. Chris Dollin

    harryajh Guest

    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

    harryajh, Feb 23, 2007
  4. 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 psigal, Feb 23, 2007
  5. Chris Dollin

    harryajh Guest

    thanks for that Lester - made a note of giving RE stuff a good bash on
    the weekend!
    harryajh, Feb 23, 2007
  6. Chris Dollin

    Z Guest

    Z, Feb 24, 2007
  7. Chris Dollin

    Z Guest


    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
  8. Chris Dollin

    Lew Guest

    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.


    - Lew
    Lew, Feb 24, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.