Reputation: 13
I have done a good many searches but have not been able to find a solution to regex a line and split the values into two variable components. I am using python2.6 and trying to figure out how to regex the integers into value variable and the text into the metric variable. The output information is pulled from a subprocess command running netstat -s.
The below match will only provide the top 6 lines but not the bottom ones where a string is first. I tried using an or conditional within the parenthesis and that did not work, tried (?P<value>[0-9]+|\w+\s[0-9]+)
I have been using this site which is really helpful but still no luck, https://regex101.com/r/yV5hA4/3#python
Any help or thoughts of using another method will be appreciated.
Code:
for line in output.split("\n"):
match = re.search(r"(?P<value>[0-9]+)\s(?P<metric>\w+.*)", line, re.I)
if match:
value, metric = match.group('value', 'metric')
print "%s => " % value + metric
What is trying to be regex:
17277 DSACKs received
4 DSACKs for out of order packets received
2 connections reset due to unexpected SYN
10294 connections reset due to unexpected data
48589 connections reset due to early user close
294 connections aborted due to timeout
TCPDSACKIgnoredOld: 15371
TCPDSACKIgnoredNoUndo: 1554
TCPSpuriousRTOs: 2
TCPSackShifted: 6330903
TCPSackMerged: 1883219
TCPSackShiftFallback: 792316
Upvotes: 1
Views: 105
Reputation: 62499
I would just forget about using re
here, and just do something like this:
for line in output.split("\n"):
value = None
metric = ""
for word in line.split():
if word.isdigit():
value = int(word)
else:
metric = "{} {}".format(metric, word)
print "{} => {}".format(metric.strip(":"), value)
One slight caveat is that any line that has two or more numbers in it will only report the last one, but that's no worse than how your current approach would deal with that case...
Edit: missed that OP is on Python 2.6, in which case, this should work:
for line in output.split("\n"):
value = None
metric = ""
for word in line.split():
if word.isdigit():
value = int(word)
else:
metric = metric + " " + word
print "%s => %s" % (metric.strip(":"), str(value))
Upvotes: 1