Get the number at the end of a paramater.

G

gerry.brennan

Hi I have a paramater which can have zero or more digits attached to
the very end.

I am looking for an expression to get this number can anyone help.

^(.+)(\d{1,2})$
This seems to be greedy ..
"timetolive33"
It will give me the 3 before the 33
Has anyone any Ideas.
 
A

Anno Siegel

Hi I have a paramater which can have zero or more digits attached to
the very end.

I am looking for an expression to get this number can anyone help.

^(.+)(\d{1,2})$
This seems to be greedy ..

Then make it non-greedy. "perldoc -q greedy" discusses this. You are
supposed to check the FAQ before asking the group (again).
"timetolive33"
It will give me the 3 before the 33

Why do you try to match the beginning of the string anyway? Simply
capture one or two digits at the end:

/(\d{1,2})$/;

Anno
 
C

Charles DeRykus

Hi I have a paramater which can have zero or more digits attached to
the very end.

I am looking for an expression to get this number can anyone help.

^(.+)(\d{1,2})$
This seems to be greedy ..
"timetolive33"
It will give me the 3 before the 33
Has anyone any Ideas.

The problem is the "." which consumes digits as well as non-digits.

Possibilities:

^(\D+)(\d{1,2})$ or maybe just ^(\D+)(\d+)$
 
A

Anno Siegel

Charles DeRykus said:
The problem is the "." which consumes digits as well as non-digits.

Possibilities:

^(\D+)(\d{1,2})$ or maybe just ^(\D+)(\d+)$

Why match the beginning of the string at all? It doesn't matter what
it contains.

Anno
 
C

Charles DeRykus

Anno said:
Why match the beginning of the string at all? It doesn't matter what
it contains.

True. I just assumed that the non-digits were wanted as well
because the OP was capturing them.
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top