# Formula to extract numbers from text

I have text strings in the form

0' - Start of straight line, Road R-6 RD 0'
397' - Road R-5 RD 0', Start of straight line
739' - Road R-149 RD 0'
0' - Start of straight line, Road R-1 RD 0', Road R-20 RD 0'

I need to extract the R- numbers and the RD values as shown in the attached file. Excel formula preferred but VBA is also acceptable.
Extract-number-and-values.xlsx
LVL 43
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
As a start you can put the following in B2 of your example and copy down:
``````=MID(A2,FIND("R-",A2)+2,FIND(" ",A2,FIND("R-",A2))-FIND("R-",A2)-2)
``````

and the following into C2 of your example and copy down
``````=MID(A2,FIND("RD ",A2)+3,FIND("'",A2,FIND("RD ",A2))-FIND("RD ",A2)-3)
``````

Columns D and E represent a second pair of R- and RD in the same data. Will they always occur in pairs? Is there a limit to the number of pairs per cell in column A?
EngineerAuthor Commented:
Thanks for the formulas. Yes they will always be in pairs. I have two at the moment and I think that the maximum could be 4.
Microsoft Excel ExpertCommented:
Hi Saqib,

please find attached solution with UDF regular expression.
EE.xlsm
Commented:
I am just curious how this can be done using formula instead of UDF
Microsoft Excel ExpertCommented:
Flora,

It is possible with formula, but the formula would be too lengthy, so if UDF does the job, why waste time with formula?

if you want this by formula, i suggest you open a new question.
Commented:
ProfJimJam is correct in that trying to do this in one long formula for the fourth instance (for example) would be too lengthy.
If you're happy to put up with some additional columns then this can be simplified somewhat as seen in the attached.

Hope that helps.
Extract-number-and-values--ss-.xlsx
EngineerAuthor Commented:
I did it myself at home and came up with this formula to be entered in b2 and copied down and across.

=IFERROR(REPLACE(REPLACE(\$A10,FIND(IF(MOD(COLUMN(),2),"'"," "),\$A10,FIND("##",SUBSTITUTE(\$A10,IF(MOD(COLUMN(),2),"RD ","R-"),"##",(COLUMN()-COLUMN(\$B10)+2)/2))),9999,""),1,FIND("##",SUBSTITUTE(\$A10,IF(MOD(COLUMN(),2),"RD ","R-"),"##",(COLUMN()-COLUMN(\$B10)+2)/2))+1,""),"")

It is big but not huge.
The regex needs a library to be loaded.

I shall keep this open a bit for someone to improve on this formula (Barry????). If not then then ProfessorJimJam's solution shall be an assist.

Experts Exchange Solution brought to you by