python - Extract substrings from logical expressions -


let's have string looks this:

mystr = '(txt_l1 (txt_l2)) or (txt2_l1 (txt2_l2))' 

what obtain in end be:

mystr_l1 = '(txt_l1) or (txt2_l1)' 

and

mystr_l2 = '(txt_l2) or (txt2_l2)' 

some properties:

  • all "txt_"-elements of string start uppercase letter

  • the string can contain more elements (so there txt3, txt4,...)

  • the suffixes '_l1' , '_l2' different in reality; cannot used matching (i chose them demonstration purposes)

i found way first part done using:

mystr_l1 = re.sub('\(\w+\)','',mystr) 

which gives me

'(txt_l1 ) or (txt2_l1 )' 

however, don't know how obtain mystr_l2. idea remove between 2 open parentheses. when this:

re.sub('\(w+\(', '', mystr) 

the entire string returned.

re.sub('\(.*\(', '', mystr) 

removes - of course - far , gives me

'txt2_l2))' 

does have idea how mystr_l2?

when there "and" instead of "or", strings different:

mystr2 = '(txt_l1 (txt_l2) , txt2_l1 (txt2_l2))' 

then can still use command above:

re.sub('\(\w+\)','',mystr2) 

which gives:

'(txt_l1  , txt2_l1 )' 

but again fail mystr2_l2. how these kind of strings?

and how 1 mixed expressions "and" , "or" e.g. this:

mystr3 = '(txt_l1 (txt_l2) , txt2_l1 (txt2_l2)) or  (txt3_l1 (txt3_l2) , txt4_l1 (txt2_l2))'   re.sub('\(\w+\)','',mystr3) 

gives me

'(txt_l1  , txt2_l1 ) or  (txt3_l1  , txt4_l1 )' 

but again: how obtain mystr3_l2?

regexp not powerful enough nested expressions (in case: nested elements in parentheses). have write parser. @ https://pyparsing.wikispaces.com/


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -