Andreas R
Andreas R

Reputation: 27

How to find a positive or negative value inside a string?

I have a Problem with a little program in Python 3.3, which should return a number from a string.

While it works well for positive values,

text = "XXXXXXX\nDateMinEnd=230\nXXXXXXX\n"
Dmin = re.search('(?<=DateMinEnd=)\w+',text)
Dmin_res = int(Dmin.group())
print(Dmin_res)
230

there is "None" result for negative values:

text = "XXXXXXX\nDateMinEnd=-230\nXXXXXXX\n"
Dmin = re.search('(?<=DateMinEnd=)\w+',text)
Dmin_res = int(Dmin.group())

'NoneType' object has no attribute 'group'

I am really a beginner, so I would appreciate any hints (and of course I had a look at http://docs.python.org/2/library/re.html before asking you and tried raw string and several special escapes, but unfortunately I am not able find the solution). Thanking you in advance.

Andreas

Upvotes: 2

Views: 3475

Answers (5)

Ilmo Euro
Ilmo Euro

Reputation: 5135

If you only need support for integers, you can add an optional - in front of your \w:

Dmin = re.search('(?<=DateMinEnd=)-?\w+',text)

Also, \w matches also non-digits, so you may want to use \d (for digit) instead:

Dmin = re.search('(?<=DateMinEnd=)-?\d+',text)

EDIT

If you need support for non-integers, use the following regex:

Dmin = re.search('(?<=DateMinEnd=)-?\d*\.?\d+',text)

Let's break it down:

-?  # optional negation
\d* # zero or more digits
\.? # optional decimal point (. is special so we need to escape it)
\d+ # one or more digits

Upvotes: 6

Burhan Khalid
Burhan Khalid

Reputation: 174662

You need to tell the regular expression that the - sign is optional, and if it exists, capture it.

So your expression should be ('(?<=DateMinEnd=)(-?\d+)'

I changed \w+ to \d+ since you are looking for numbers, not words. The -? makes the - optional, and the ( ) surrounding the expression make it into a capture group.

So now you get:

>>> re.search('(?<=DateMinEnd=)(-?\d+)', text).group()
'-230'

Upvotes: 0

Blender
Blender

Reputation: 298392

- isn't captured by \w. I would just do something like this:

Dmin = re.search('DateMinEnd=(.*)', text)

Upvotes: 1

suhailvs
suhailvs

Reputation: 21720

- is not a word(\w+)

to search all types, replace \w+ with .+

Upvotes: 0

orlp
orlp

Reputation: 117771

Try adding an optional minus sign in the regular expression:

Dmin = re.search('(?<=DateMinEnd=)-?\w+', text)

- is the minus sign itself, and ? marks that there may be one or zero minus symbols (in other words it's optional).

Upvotes: 5

Related Questions