The returned string inside the string based on the expression 'x = {(. *)}'

I have a line that can change, but will always contain x={stuffNeeded} .

For example: n=1,x={y,z,w},erore={3,4,5} or x={y,z,w} or erore={3,4,5},x={y,z,w} , etc.

I have damn time to figure out how to get y,z,w . The closest I found to find the answer was based on Yathart's answer to this other message. Regular expression to return all characters between two special characters .

In my search, I still came across something that almost worked. Testing was done here http://rubular.com/r/bgixv2J6yF and in python.

This has been tested in python using:

 i='n=1,x={y,z,w},erore={3,4,5}' j='n=1,x={y,z,w}' print re.search('x={(.*)}',i).group(1) print re.search('x={(.*)}',j).group(1) print re.search('x={(.*)}.',i).group(1) print re.search('x={(.*)}.',j).group(1) 

Result for four different images:

 'y,z,w' 'y,z,w},erore={3,4,5' AttributeError: 'NoneType' object has no attribute 'group' 'y,z,w' 

Required result: 'y,z,w' for all cases, and then if x={*} was not really found, I would put an error.

Thanks in advance.

+5
source share
4 answers

This regex does what you are trying to do:

 regex = r'x={([^\}]*)}' 

Live demo here

Explanation

  • {([^\}]*) : find the open bracket, then find (and write down) any number of characters } . So your group 1 will contain the captured values ​​for x.
  • } : find the closing brackets
+6
source

The main problem is that {(.*)} Matches the longest line starting with { and ending with } , which in some cases is y,z,w},erore={3,4,5

Can you use unwanted matching by adding ? . You do not need another case.

 import re i='n=1,x={y,z,w},erore={3,4,5}' j='n=1,x={y,z,w}' expr = 'x={(.*?)}' print (re.search(expr,i).group(1)) print (re.search(expr,j).group(1)) 

result:

 y,z,w y,z,w 
+3
source

Using re.findall :

 >>> import re >>> re.findall('x={[^\}]*}', s) 

#driver:

 IN : s = 'n=1,x={y,z,w},erore={3,4,5}' OUT : ['x={y,z,w}'] IN : s = 'n=1,x={y,z,w}' OUT : ['x={y,z,w}'] IN : s = 'x={y,z,w}' OUT : ['x={y,z,w}'] 

Now, to get the value of x, y, z , use split and strip :

 >>> l = re.findall('x={[^\}]*}', s) #if `l` is not empty >>> out = l[0] => 'x={y,z,w}' >>> y, z, x = out.strip('x={}').split(',') >>> y, z, x => ('y', 'z', 'w') 
+1
source

You can try the following:

 import re s = 'n=1,x={y,z,w},erore={3,4,5}' final_data = re.findall('=\{(.*?)\}', s) 

Output:

 ['y,z,w', '3,4,5'] 
+1
source

Source: https://habr.com/ru/post/1272884/


All Articles