0

I have this string - E1(05): 12 13 16 18 24 E2(01):13 - and all i wish back is:

  • the contents after the E1 i.e 05,
  • the list of the 5 numbers i.e. 06 08 12 18 29,
  • the contents after the E2 i.e. 01,
  • and lastly, the last number i.e. 13.

I have tried the 're' function but it returns hex string and cannot find examples to strip/split this out with success. There MUST be an elegant way of solving this.

3
  • from this example, what i understand is , you just need all numbers from string, right ? Commented Jan 25, 2017 at 13:14
  • "...the list of the 5 numbers i.e. 06 08 12 18 29" Where did you get these numbers from? Commented Jan 25, 2017 at 13:16
  • what should be matched from this E1(05): 12 13 16 18 24 E2(01):13 input: [05, 01] ? Commented Jan 25, 2017 at 13:16

3 Answers 3

2

Here is one way using re.findall() by using word boundaries around \d+ (in order to not match 1 in E1, etc):

In [9]: s = "E1(05): 12 13 16 18 24 E2(01):13"

In [10]: re.findall(r'\b\d+\b', s)
Out[10]: ['05', '12', '13', '16', '18', '24', '01', '13']
Sign up to request clarification or add additional context in comments.

Comments

0

You can do following

data = re.findall(r'\b\d+\b','YOUR_STRING')
ans = [int(i) for i in data]

Hope this helps !

3 Comments

This doesn't work. ans is [1, 5, 12, 13, 16, 18, 24, 2, 1, 13]
You just need \b to filterout those E1 stuff. Sorry i didn't recognize at first
Right, but now your answer is essentially a duplicate of Kasramvd's. The only difference is that you explicitly convert the matches to integers, and it's unclear if that's what OP wants.
0

Considering you want to get the id of that E token and the information after that, here's a simple solution:

import re

data = "   E1(05): 12 13 16 18 24 E2(01):13 "

for v in data.split("E"):
    if v.strip() == '':
        continue

    m = re.match(r'E.*?\((.*?)\):(.*)', 'E' + v)
    e_id, e_data = m.group(1), [int(v) for v in m.group(2).split()]
    print(e_id, e_data)

Then you'd get something like this:

('05', [12, 13, 16, 18, 24])
('01', [13])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.