string split by first occurens number

J

John Devlon

Hi,

Does anyone have a solution for the following problem:
I would like to split a string into 2 strings by the first occurense of a
number.

Example:

"Londen street 52" into "Londen street" and "52"
"New york harbor 52 dock 2" into "New york harbor" and "dock 2"

Does anyone have a solution?

Thanx

John



__________ Informatie van ESET Smart Security, versie van database viruskenmerken 5067 (20100428) __________

Het bericht is gecontroleerd door ESET Smart Security.

http://www.eset.com
 
A

Andrew Morton

John said:
Does anyone have a solution for the following problem:
I would like to split a string into 2 strings by the first occurense
of a number.

Example:

"Londen street 52" into "Londen street" and "52"
"New york harbor 52 dock 2" into "New york harbor" and "dock 2"

Your second example suggests that if there is more text after the number
then the number is to be omitted from the result. Is this correct?
 
S

Saga

Andrew brings up a goo dpoint. Your first example:
"Londen street 52" into "Londen street" and "52"

This implies that "New york harbor 52 dock 2" should be
turned into "New york harbor" and "52 dock 2". Saga
 
F

Family Tree Mike

Hi,

Does anyone have a solution for the following problem:
I would like to split a string into 2 strings by the first occurense of a
number.

Example:

"Londen street 52" into "Londen street" and "52"
"New york harbor 52 dock 2" into "New york harbor" and "dock 2"

Does anyone have a solution?

Thanx

John



__________ Informatie van ESET Smart Security, versie van database viruskenmerken 5067 (20100428) __________

Het bericht is gecontroleerd door ESET Smart Security.

http://www.eset.com

Depending on your answer to the question regarding keeping the 52 in the
first example, then the following regular expression should work.

(.*?)(?:\d+)(.*)
 
J

John Devlon

Yes, you are right, my mistake.
The second should be:
"New york harbor 52 dock 2" into "New york harbor" and "52 dock 2"

Sorry
 
F

Family Tree Mike

Depending on your answer to the question regarding keeping the 52 in the
first example, then the following regular expression should work.

(.*?)(?:\d+)(.*)

I see your reply to another post that you want the number in the second
string. This means the reqular expression should be:

(.*?)(\d+.*)

The earlier example did not capture the number portion in the second
capture.
 

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

Top