How to extract number from string?

I'd like to extract the price (30.00) from the following string:


2014 HLF NOV 22 30.00 PUT


Price will always be after the 4th space. I have several of these (option tickers) that I'd like to perform this function on. Anyone know what the formula looks like to do this?

Posted on Oct 6, 2014 7:54 PM

Reply
25 replies

Oct 6, 2014 8:53 PM in response to 4thSpace

Hi 4th Space,


This method uses FIND to look for positions of all 5 spaces, then MID to pull out the price. This will work for any length words before the price.



1st space

2nd space

3rd space

4th space

5th space

price

2014 HLF NOV 22 30.00 PUT

5

9

13

16

22

30.00


Formula in B2

=FIND(" ",$A2,1)

(The 1 is not required, but I put it in for consistency)


Formula in C2

=FIND(" ",$A2,B2+1)


Formula in D2

=FIND(" ",$A2,C2+1)


Formula in E2

=FIND(" ",$A2,D2+1)


Formula in F2

=FIND(" ",$A2,E2+1)


Formula in G2

=MID(A2,E2+1,F2−E2)


Regards,

Ian.

Oct 7, 2014 10:54 AM in response to 4thSpace

Hi 4thSpace,


I think Ian's solution meets your citeria. I am wondering if it is possible to make a close guess as to where that 4th space is likely and eliminate some of the columns. All my formulas are based on Ian's.


Here are some assumptions. Are they true?

a. 2014- always 4 characters

b. NOV- always 3 chars

c. 22- always 2 chars

d. HLF- 3-4 characters


If a-c are true and d is either 3 or 4 chars then

=FIND(" ",$A1,15)

will find that 4th space and we don't need columns b-d

If the date is sometimes 1 char and HLF can be 2 chars then we cannot predict where the 4th space will be reliably without additional columns.


=FIND(" ",$A1,18) will find the 5th space if the price is at least 4 chars (1.00) even if d and c are too short for the above.

User uploaded file

In Row 1 we have the reduced columns- the formulas should have "=" in front of them.

In Row 2 I have bundled it all together for you.

Here is a copy and paste

2014 HLF NOV 22 30.00 PUT

FIND(" ",$A1,15)

FIND(" ",$A1,18)

MID(A1,B1,C1−B1)

2014 HLF NOV 22 30.00 PUT

30.00







B2 =MID($A2,FIND(" ",$A2,15),FIND(" ",$A2,18)−FIND(" ",$A2,15))


quinn


ps these solutions both provide a text result. If you want to do calculations with these you will need to use VALUE().

Oct 8, 2014 8:29 PM in response to 4thSpace

AppleScript can provide a quick and clean solution (no extra columns). Copy the script below and paste into AppleScript Editor. Then just select the cells containing the option prices, then hit the green triangle run button, click once in a cell where you want the numbers (strike price?) to start, and command-v to paste.


SG


tell application "Numbers" to tell front document's active sheet

set t to first table whose selection range's class is range

set s to ""

repeat with c in t's selection range's cells

set v to c's value

set s to s & v's fifth word & return

end repeat


set the clipboard tos

end tell

Oct 9, 2014 3:29 AM in response to T2YUKI

Hi T2YUKI,


Like!


Our next challenge is to find the price if it is not always 5 characters long. Easy to find the position of the decimal point with

=SEARCH(".",A2)


String

Decimal Point

2014 HLF NOV 22 30.00 PUT

19

2014 HLF NOV 22 300.00 PUT

20

2014 HLF NOV 22 9.00 PUT

18

2014 HLF NOV 22 8.50 PUT

18

2014 Gobbledygook NOV 22 30.00 PUT

28


But if the price is not 5 characters long, or if the preceding text should vary, we must work backwards from the position of the decimal point to find the space before the price.


An interesting problem!


Regards,

Ian.

Oct 9, 2014 5:08 AM in response to T2YUKI

Hi T2YUKI,


I like SGIII's AppleScript solution (find the fifth word). Neat solution, SG! 🙂.


In Numbers, (without AppleScript) it is an interesting challenge.


This forum does not always show screen shots. I have given up on screen shots, and this table was copied and pasted from Numbers. To make it easier when referring to cells, I have added another column (new column A) and another row (row 1).


A

B

C

D

E

F

Row

String

Decimal Point

Price

Next Space

Previous Space?

3

2014 HLF NOV 22 30.00 PUT

19

30.00

22


4

2014 HLF NOV 22 300.00 PUT

20

00.00

23


5

2014 HLF NOV 22 9.00 PUT

18

9.00

21


6

2014 HLF NOV 22 8.50 PUT

18

8.50

21


7

2014 Gobbledygook NOV 22 30.00 PUT

28

30.00

31



Formula in C3 (and Fill Down

=SEARCH(".",B3)

(Assuming that the decimal point is the first "dot" in A3 B3)


Formula in D3 (and Fill Down)

=MID(B3,SEARCH("??.??",B3),5)

But that misses the first character (3) in 300.00 and adds the space before 9.00 and 8.50


Formula in E3 (and Fill Down)

=SEARCH(" ",B3,C3)

That is, look for the next space in B3, starting from the decimal point.


Previous space is our next challenge.


... I just saw an email reply from Wayne...


copy the data into a text editor and replace all the spaces with tabs then copy and paste back into Numbers. each item (field) will now be in its' own column


How simple! Thanks, Wayne! 🙂.


Regards,

Ian.


Edit (Assuming that the decimal point is the first "dot" in A3 B3)

Oct 9, 2014 8:46 AM in response to 4thSpace

Hi,


To get the text between the 4th space and the 5th space, you can use this formula:


=SUBSTITUTE(RIGHT(B2,(LEN(B2)−(SEARCH("|",SUBSTITUTE(B2," ","|",4),1)))),RIGHT(B2,(LEN(B2)−(SEARCH("|",SUBSTITUTE(B2," ","|",5),1) − 1))),"",1)


We must ensure that these three characters | does not exist in the string, otherwise change these characters in the formula.

Oct 10, 2014 11:53 AM in response to SGIII

Quote: "AppleScript really *is* simple (sometimes)."

Perhaps I should have raised this in the OS X Forum, but for the moment I am focussed on using Numbers.

In the last few days I have been considering learning about Applescript.

I suppose what I'm thinking of is how far does it make things easier perhaps to "open up" Numbers perhaps more than the available functions - although they are quite comprehensive.

Any guidance would be useful. I presume it is generally applicable across MAC OS and utilities.

Regards

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to extract number from string?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.