Regular expression issue

Discussion in 'Java' started by simon_s_li@hotmail.com, Jan 19, 2007.

  1. Guest

    Hi,

    I am trying to extract some letters from a piece of string, but I do
    not know how many letters I need to extract.

    All I know is that there are letters that follow the letter:

    Example:

    At the moment I am using the following regualr expression:

    (XX|YY)(\w\w)(\d{1,15})

    Two example strings are:

    EXAMPLE 1 - XXAB123456789
    EXAMPLE 2 - YYABC123456789

    I know I can get the XX or YY

    However in both example strings I need to get AB or ABC.

    However my regular expression (XX|YY)(\w\w)(\d{1,15}) will only get AB
    in EXAMPLE 1 which is correct
    but it will only get AB in EXAMPLE 2 and not ABC.

    If I use (XX|YY)(\w\w\w)(\d{1,15}) it will get AB1 in EXAMPLE 1 rather
    than AB.

    Can anyone help me?

    Regards
    Simon
     
    , Jan 19, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I am trying to extract some letters from a piece of string, but I do
    > not know how many letters I need to extract.
    >
    > All I know is that there are letters that follow the letter:
    >
    > Example:
    >
    > At the moment I am using the following regualr expression:
    >
    > (XX|YY)(\w\w)(\d{1,15})
    >
    > Two example strings are:
    >
    > EXAMPLE 1 - XXAB123456789
    > EXAMPLE 2 - YYABC123456789
    >
    > I know I can get the XX or YY
    >
    > However in both example strings I need to get AB or ABC.
    >
    > However my regular expression (XX|YY)(\w\w)(\d{1,15}) will only get AB
    > in EXAMPLE 1 which is correct
    > but it will only get AB in EXAMPLE 2 and not ABC.
    >
    > If I use (XX|YY)(\w\w\w)(\d{1,15}) it will get AB1 in EXAMPLE 1 rather
    > than AB.
    >
    > Can anyone help me?
    >
    > Regards
    > Simon
    >


    \w+

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 19, 2007
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to extract some letters from a piece of string, but I do
    > not know how many letters I need to extract.
    >
    > All I know is that there are letters that follow the letter:
    >
    > Example:
    >
    > At the moment I am using the following regualr expression:
    >
    > (XX|YY)(\w\w)(\d{1,15})
    >
    > Two example strings are:
    >
    > EXAMPLE 1 - XXAB123456789
    > EXAMPLE 2 - YYABC123456789
    >
    > I know I can get the XX or YY
    >
    > However in both example strings I need to get AB or ABC.
    >
    > However my regular expression (XX|YY)(\w\w)(\d{1,15}) will only get AB
    > in EXAMPLE 1 which is correct
    > but it will only get AB in EXAMPLE 2 and not ABC.
    >
    > If I use (XX|YY)(\w\w\w)(\d{1,15}) it will get AB1 in EXAMPLE 1 rather
    > than AB.
    >
    > Can anyone help me?


    If I understand correctly, all the strings start with either "XX" or
    "YY", and then they have some unknown number of uppercase letters from the
    latin alphabet (e.g. A to Z), followed by 1 to 15 digits (e.g. 0 to 9).

    So how about something like:

    (XX|YY)([A-Z]+)(\d{1,15})

    or

    (XX|YY)([A-Z]*)(\d{1,15})

    Depending on whether you allow for 0 letters other than XX and YY or not. Or
    if you know you always have 2 or 3 letters other than XX and YY, then:

    (XX|YY)([A-Z]{2,3})(\d{1,15})

    - Oliver
     
    Oliver Wong, Jan 19, 2007
    #3
    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,379
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    881
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    12,046
    vforvikash
    Dec 29, 2008
  4. Replies:
    2
    Views:
    273
    Sibylle Koczian
    Jul 24, 2006
  5. mac1sam

    Issue with Regular expression

    mac1sam, Sep 4, 2007, in forum: Java
    Replies:
    0
    Views:
    300
    mac1sam
    Sep 4, 2007
Loading...

Share This Page