-4

Pulling some trading data and having issues using regex to separate tickers and percentage of holding

Inputs

"94324.13%"

"007007.13%"

"0354202.91%"

Desired Output

"9432|4.13%" (ticker is 4 numbers)

"00700|7.13%" (ticker is 5 numbers)

"035420|2.91%" (ticker is 6 numbers)

Main issue is that the number of digits the ticker is may vary anywhere from 4-6 digits.

  • Do you have any way of knowing how long the tickers are or the other part is? – Oisin Mar 06 '16 at 00:24
  • Where is the regex, you have tried so far? – Farside Mar 06 '16 at 01:02
  • In which language are you using? Javascript, python, php etc? – Saleem Mar 06 '16 at 01:03
  • Unless you know how long the ticker is, or can assume how long the % suffix is, you cannot umabiguously parse this data. eg.something like "654321.09%" may be a a 4 or 5 digit ticker, etc. – Rook Mar 06 '16 at 10:23

2 Answers2

1

With the given information it is not possible to have a 100% accurate split of the two parts. For instance:

123410.05%

... could split in either of the following two:

1234|10.05%
12341|0.05%

And if percentages might not have a zero before the decimal point, then this would also be a possible split:

123410|.05%

The following regex replace will assume the percentage has one digit before the decimal point, and possibly a minus sign:

Find:

/^(\d{4,6})(\-?\d.*)$/gm

Replace:

\1|\2

See it on regex101.com.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

I'd like to try this regex

(\d{4,6})(\d+\.\d{1,2}%)

Here is full demo:

Python:

data = "007007.13%"
rx = re.compile(r"(\d{4,6})(\d+\.\d{1,2}%)")
formated_text = rx.sub(r'\1|\2', data)
print formated_text

#it will print
00700|7.13%

You can look demo in python here

Javascript:

var re = /(\d{4,6})(\d+\.\d{1,2}%)/g; 
var str = '"007007.13%"';
var subst = '$1|$2'; 

var result = str.replace(re, subs);

Demo in Javascript

Saleem
  • 8,728
  • 2
  • 20
  • 34