Hi all,
I want to parse or extract integers out of a string using regular expression i need your help in forming the group.
Example.
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 12.123.177.174/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.
How about "(\\d+) packets ((input|output))"? Those parentheses around \\d+ and (input|output) mean that they are capturing groups; that way you can retrieve them using a Matcher:
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.
Hope you understood my problem.
final String regex = "([^\\s]*[0-9]*)\\s*packets\\s*input.*"+
"\\s*([^\\s]*[0-9]*)\\s*packets\\s*output";
The above regex is written by me its takes both the integers before packets input as well as packets output only if it is coming in two seperate lines like below
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.
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)
in the above output group 0 takes integer with packets output and group1 takes the exact integer . Similarly i need for the integers before packets input also.
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.
in the above output group 0 takes integer with packets output and group1 takes the exact integer . Similarly i need for the integers before packets input also.
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.
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.
Well, your regex doesn't specify this either...
Henry
This message was edited 1 time. Last update was at by Henry Wong