5 Replies - 653 Views - Last Post: 29 September 2012 - 10:23 AM Rate Topic: -----

#1 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Regex

Posted 15 September 2012 - 09:21 AM

I have my string, but whenever I do a regex on it turns up with None.

Here's my code:

def regex(product):
	p = re.compile('\d{2}%')
	result = p.match(product)
	print result
	return result



My input is something like this:
<em id="productYoyo_Relative">-25%</em>

The -25% changes all the time. So I just want to grab the 25 for comparison.

I've looked at Python's docs for regex, that's how I wrote what I have. Also, I tested my regular expression on gskinner and it worked there. Am I not implementing Python's regex correctly?

This post has been edited by Gamegoofs2: 15 September 2012 - 09:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Regex

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Regex

Posted 15 September 2012 - 10:18 AM

The problem is first, you want to capture the groups that you want ( ) <--- with brackets

Second I am assuming you need to account for the minus that may or may not be there if it could be anything then just remove that character.

This will work:
def regex(product):
	p = re.compile('-?(\d{2})%')
	result = p.match(product)
	return result
....
>>> m = regex("-25%")
>>> print m.group(1)
25
>>> p = regex("25%")
>>> print p.group(1)
25


The -? means 0 or 1 minus signs so if a character is at the front, it must be a minus.

Next, (\d{2})% captures the two digits in brackets and the percent outside.

You must print the "groups" of the match. In "-25%" group 0 is: "-25%" and group 1 is the bracketed match "25"

If you need more than 2 digits change the \d{2} to \d+ or a range of values \d{2, 5}

Hope that helps :)

This post has been edited by Simown: 15 September 2012 - 10:19 AM

Was This Post Helpful? 2
  • +
  • -

#3 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Re: Regex

Posted 15 September 2012 - 10:44 AM

It works when I just put in "-25%" or "25% but when I try it with <em id="productYoyo_Relative">-25%<em> I get ArrtibuteError: 'NoneType' object has no attribute 'group'

It seems since there is other text there that it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Regex

Posted 15 September 2012 - 10:54 AM

You need to match any other characters before and after so:
def regex(product):
	p = re.compile('.*-?(\d{2})%.*')
	result = p.match(product)
	return result


Will match all strings with some numbers in the middle in that format. The .* means to match any character any amount of times.
>>> x = regex('<em id="productYoyo_Relative">-25%</em>')
>>> x.group(1)
...
25


Was This Post Helpful? 3
  • +
  • -

#5 Gamegoofs2  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Re: Regex

Posted 17 September 2012 - 11:12 AM

Thanks again Simown! You're very helpful :)
Was This Post Helpful? 0
  • +
  • -

#6 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Regex

Posted 29 September 2012 - 10:23 AM

Actually, you can keep that regex simple if you use p.search instead of p.match.

match matches things from the start of the line.
search looks through the entire line.
The other behavior is identical.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1