Regular expression issue

Discussion in 'Javascript' 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


    Hi

    Look at character ranges -> e.g. /[A-Z]/

    Thus

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

    or

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

    Regards

    Julian Turner
    Julian Turner, Jan 19, 2007
    #2
    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,272
  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:
    831
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,921
    vforvikash
    Dec 29, 2008
  4. Replies:
    2
    Views:
    259
    Sibylle Koczian
    Jul 24, 2006
  5. Replies:
    2
    Views:
    303
    Oliver Wong
    Jan 19, 2007
Loading...

Share This Page