0

I have a variable Field in which a string is store like this:

Field= "In Field 'fieldname':(Value1) has changed from (1) to (2)"

From this string stored in variable Field, I want to catch the values (1) and (2) in two different varibales like:

OldValue=1
NewValue=2

Can someone please help me here? I am handling this variables in Python

1
  • Tagging python is fine, but tagging both python2.7 and python-3x doesn't make sense, which one are you using? Commented Apr 21, 2013 at 10:33

1 Answer 1

2

This finds digits surrounded by brackets:

>>> import re
>>> Field= "In Field 'fieldname':(Value1) has changed from (1) to (2)"
>>> OldValue, NewValue = map(int, re.findall(r'\((\d+)\)', Field))
>>> OldValue
1
>>> NewValue
2

You may not only have two values, in which case

vals = list(map(int, re.findall(r'\((\d+)\)', Field)))

will do the trick

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

9 Comments

What changes I have to do if I want to catch: name=fieldname
Do you want to catch :? Like :(Value1) as well as the (1) (2)?
Also your trick works if a I have int values, suppose if old value and new value is not int, then what will be the changes?
No I want to cath in name = filedname and change values(also if they are not of type int)
@hulk007 If they aren't int, re.findall(r'\((\d+)\)', Field) alone, returns them as strings
|

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.