Regular expression issue

S

simon_s_li

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
 
K

Knute Johnson

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+
 
O

Oliver Wong

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
 

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

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top