Reputation: 4137
I am trying to parse the results of the TSHARK capture
Here is the line I am filtering on:
Internet Protocol, Src: 10.10.52.250 (10.10.52.250), Dst: 224.0.0.2 (224.0.0.2)
I am trying to extract the Src and Dst,
Here is my code to do that:
str(re.search("Src:\s[0-9\.]{7-15}", a, re.I|re.M).group())[5:]
str(re.search("Dst:\s[0-9\.]{7-15}", a, re.I|re.M).group())[5:]
I keep getting no match when I run this, but when I use the exact length of the IP address in the regular expression it works. ie
str(re.search("Src:\s[0-9\.]{9}", a, re.I|re.M).group())[5:]
Works just fine. How do I fix this problem?
Upvotes: 0
Views: 2839
Reputation: 71
Not very nice:
text = 'fasga@fas#2*^127.0.0.1tfgws5151'
pattern = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
pattern_re = re.compile(pattern)
ip_address = pattern_re.findall(text)
Upvotes: 2
Reputation: 26940
for match in re.finditer(r"\((\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))\b\)", subject):
This should match any IPV4 IP address. Actual IPs are captured into group 1.
While your regex may work, it is dangerous because 999.999.999.999 is not a valid but it will match.
Upvotes: 3
Reputation: 52778
Try using {7,15}
:
str(re.search("Src:\s[0-9\.]{7,15}", a, re.I|re.M).group())[5:]
str(re.search("Dst:\s[0-9\.]{7,15}", a, re.I|re.M).group())[5:]
Also, you should check that a group exists first (or put a try
/except
around it):
groupFound = re.search("Src:\s*[\d\.]{7,15}", a, re.I | re.M)
if groupFound:
str(groupFound.group())[5:]
# or:
try:
str(re.search("Src:\s*[\d\.]{7,15}", a, re.I | re.M).group())[5:]
except AttributeError:
# handle it
Upvotes: 0