1

I need to parse this output :-

S1-link-id   eNB-IP-Address   MME-IP-Address    Facing  State
-------------------------------------------------------------------
303          141.1.1.2        191.1.1.2         eNodeB  Established
301          141.1.1.2        191.1.1.2         MME     Established
306          141.1.1.3        191.1.1.2         eNodeB  Established
304          141.1.1.3        191.1.1.2         MME     Established
309          141.1.1.4        191.1.1.2         eNodeB  Established
307          141.1.1.4        191.1.1.2         MME     Established

I want to get multiple values for a single id (first column). For "303" - I need enb,mme ip addresses, facing and state values, same way for other ids.

regex for one desired output :-

\s*(?P<id>\d+)\s+(?P<enb_adr>\d+\.\d+\.\d+\.\d+)\s+(?P<mme_adr>\d+\.\d+\.\d+\.\d+)\s+(?P<facing>\w+)\s+(?P<state>\w+)\s*

Got stuck on how to proceed after this to get desired values for whole output.

2 Answers 2

5

Looks like your regular expression is alright, so all you need to do is use re.findall():

import re
print re.findall(r'\s*(?P<id>\d+)\s+(?P<enb_adr>\d+\.\d+\.\d+\.\d+)\s+(?P<mme_adr>\d+\.\d+\.\d+\.\d+)\s+(?P<facing>\w+)\s+(?P<state>\w+)\s*', the_text_above)

Returns:

[('303', '141.1.1.2', '191.1.1.2', 'eNodeB', 'Established'), ('301', '141.1.1.2', '191.1.1.2', 'MME', 'Established'), ('306', '141.1.1.3', '191.1.1.2', 'eNodeB', 'Established'), ('304', '141.1.1.3', '191.1.1.2', 'MME', 'Established'), ('309', '141.1.1.4', '191.1.1.2', 'eNodeB', 'Established'), ('307', '141.1.1.4', '191.1.1.2', 'MME', 'Established')]

Sign up to request clarification or add additional context in comments.

Comments

0

Even though @Haidro already answered the question, I wanted to impart a helpful suggestion for really long regexes like you have there. Comment your regex breakdown!

I'll be the first to admit, I suck at regex. I always come back to examples like this and thank 'past me' for being diligent with documentation. The key is to use flags=re.VERBOSE.

_brokendown_for_joo = re.compile("""
        get
            (As                 #/1
                (Int            #/2
                |Float
                |Boolean
                |String
                |StringList
                |Char)
            |Required
            )?
        [(]                     #opening paren       
            ['\"]               #single or double quotes
                (\w+)           #Anyword   /3
            ['\"]               #single or double quotes
                ,\s*            #comma and optional spaces
            ['\"]               #single or double quotes
                (\w+)           #Anyword  /4
            ['\"]               #single or double quotes
                (,\s*           #comma and optional spaces  /5
                (\w+            #anyword  /6
                |['][^']+[']    #or any chars in single quotes
                |[\"][^\"]+[\"] #or any chars in double quotes
                ))?
            \s*                 #optional spaces
        [)]                     #closing paren
        """, flags=re.VERBOSE)

Much easier to read than this.

re.compile(r"(%s)[.]get(As(Int|Float|Boolean|String|StringList|Char)|Required)?[(]['\"](\w+)['\"],\s*['\"](\w+)['\"](,\s*(\w+|['][^']+[']|[\"][^\"]+[\"]))?\s*[)]")

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.