-5

I would like to have a regex to separate a word and decimal number.

Example of an input string:

{[FACEBOOK23.1K],[UNKNOWN6],[SKYPE12.12M]} 

Expected result:

{[FACEBOOK,23.1K],[UNKNOWN,6],[SKYPE,12.12M]}

So curently i am using re.findall(r'\D+|\d+.+\d+|\d+',element)

and here element is [FACEBOOK23.1K] and it is seperating as [FACEBOOK,23.1,K]

and i tried the followig regex =re.findall(r'\D+|(\d+.+\d+)?(K|M|G|T)|(\d+)?(K|M|G|T)|\d+',element) to get expected result as it is showing the seperation correctly on regex online simulator ,but it is not working when i am trying on my code ? >>>>

1 Answers1

1
#!/usr/bin/python2
# -*- coding: utf-8 -*-

import re

text = '{[FACEBOOK23.1K],[UNKNOWN6],[SKYPE12.12M]}';

m = re.findall('\[([a-z]+)([^\]]+)\]', text, re.IGNORECASE);

output = '{';
i = 0
for (name,count) in m:
    if i>0:
        output += ','
    output += "["+name+","+count+"]"
    i=i+1
output += '}'

print output
ZiTAL
  • 3,466
  • 8
  • 35
  • 50
  • if i have text like text = ['FACEBOOK23.1K','UNKNOWN6','SKYPE12.12M']; and seperating like ['FACEBOOK,23.1K','UNKNOWN,6','SKYPE,12.12M'] what can be regex? – Rahul surya Feb 27 '17 at 09:48
  • I wrote the regex inside the findall function – ZiTAL Feb 27 '17 at 10:08
  • Yeah i got it and tried the above scenerio also #!/usr/bin/python2 # -*- coding: utf-8 -*- import re text = ['IP_ICMP_HELLO23.1K','UNKNOWN6','SKYPE12.12M']; ele_list1=[] for i in text: print i m = re.findall('[(a-z)(_)]+|[^\]]+', i, re.IGNORECASE); ele_list1.append(m) print ele_list1 – Rahul surya Feb 27 '17 at 10:25
  • I got the output ad executing as follows something like this http://ideone.com/N6MzWd – Rahul surya Feb 27 '17 at 11:51