I want to parse or extract integers out of a string using regular expression i need your help in forming the group.
I have got a string like this which comes as an output from someother method in the same order as below
show int sw1.3437 Switch1.3437 is up, line protocol is up Hardware is Mxt4700 Based ATM PA Description: "Uplink HUB PVC to PER whplny90p12-l437" Internet address is 188.8.131.52/30 MTU 4470 bytes, BW 1197656 Kbit, DLY 100 usec, reliability 255/255, txload 42/255, rxload 44/255 Encapsulation ATM 70854799525 packets input, 19151340856378 bytes 73032762307 packets output, 34009367972443 bytes 0 OAM cells input, 0 OAM cells output Last clearing of "show interface" counters 1y6w whplny90h10-z4 41#
from this string i have to take the integer "70854799525" which is before packets input and the integer " 73032762307" which is before packets output. I need to extract these two integers from the above string.
In short i need to extract the integer which is coming before packets input and packets output.
I know you asked for regular expressions, but why not simply use string functions? Use "packets input" and "packets output" as delimeters to find the right position (with indexOf()), then scan backward from there.
Joined: Jun 23, 2009
I know it can be done using substring method but the requirement is i have to take only the integers which is coming before packets input and packets output. Mean to say that integer may or may not come.
that is the reason i chose to use regular expression.
Raj developer wrote:But the regular expression which you gave only takes the integer which is before packets output.
But in my case i have to take both the integers before packets input and packets output in one traverse.
Have you even tried it? I did on your input string, and it found both matches.
The regular expression is built as follows:
- "(\\d+)" means 1 or more digits. The parentheses means that a match will show up as a separate group (group 1 in this case)
- " packets " means the exact string " packets "
- "((input|output)) means either input or output. The inner parentheses are for ensuring the | (or) is bound to only "input" and "output"; concatenation has a higher precedence than |. The outer parentheses means that a match will show up as a separate group (group 2 in this case)
i tried your code but it still gave only integer before packets output
That's because "input" doesn't match -- you specify exactly one space between "packet" and "input|output", but your source string has more than one space between the two words. If you want more than one space to match too, you need to specify that.
Please note that in my input string the packets input and packets output are in two different lines.
I need the regex which will take the integer even if it is coming in a single line or multiline.