I have a string that can vary 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 am having a devil of a time figuring out how to get y,z,w
. The closest I got to finding the answer was based off of Yatharth's answer on this other post Regular expression to return all characters between two special characters.
It my searching I've so far come across something that almost worked. Testing was done here http://rubular.com/r/bgixv2J6yF and in python.
This was 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 the four different print:
'y,z,w'
'y,z,w},erore={3,4,5'
AttributeError: 'NoneType' object has no attribute 'group'
'y,z,w'
Needed result is 'y,z,w'
for all cases and then if x={*}
really isn't found I would put an error catch.
Thank you in advance.
It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.
Python Substring Before Character You can extract a substring from a string before a specific character using the rpartition() method. rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.
To convert a string to integer in Python, use the int() function. This function takes two parameters: the initial string and the optional base to represent the data. Use the syntax print(int("STR")) to return the str as an int , or integer.
This regex does what you're trying to do :
regex = r'x={([^\}]*)}'
Live demo here
Explanation
{([^\}]*)
: look for an opening bracket, then look for (and capture) any number of non }
characters. So, your group 1 will contain the captured values for x.}
: look for a closing bracketThe main problem is that {(.*)}
matches the longest string starting by {
and ending by }
, which in some cases is y,z,w},erore={3,4,5
You could use non-greedy matching by adding ?
. You don't need any other 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With