Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return string within a string based on expression 'x={(.*)}'

Tags:

python

regex

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.

like image 509
Scott G Avatar asked Oct 25 '17 15:10

Scott G


People also ask

What is ?: In regex?

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.

How do I find a string before a specific character in Python?

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.

How do you return a string to an int in Python?

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.


2 Answers

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 bracket
like image 161
Ashish Ranjan Avatar answered Oct 16 '22 08:10

Ashish Ranjan


The 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
like image 36
Jean-François Fabre Avatar answered Oct 16 '22 10:10

Jean-François Fabre