Question

for some reason when I get regex to get the number i need it returns none. But when I run it here http://regexr.com/38n3o it works

the regex was designed to get the last number of the ip so it can be removed

lanip=74.125.224.72
notorm=re.search("/([1-9])\w+$/g", lanip)
Was it helpful?

Solution

That is not how you define a regular expressions in Python. The correct way would be:

import re
lanip="74.125.224.72"
notorm=re.search("([1-9])\w+$", lanip)
print notorm

Output:

<_sre.SRE_Match object at 0x10131df30>

You were using a javascript regex style. To read more on correct python syntax read the documentation

If you want to match the last number of an IP use:

import re
lanip="74.125.224.72"
notorm=re.search("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", lanip)
print notorm.group(4)

Output:

72

Regex used from http://www.regular-expressions.info/examples.html

Your example did work in this scenario, but would match a lot of false positives.

OTHER TIPS

What is lanip's type? That can't run.

It needs to be a string, i.e.

lanip = "74.125.224.72"

Also your RE syntax looks strange, make sure you've read the documentation on Python's RE syntax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top