This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regular expression to take integers out of a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression to take integers out of a string" Watch "Regular expression to take integers out of a string" New topic
Author

Regular expression to take integers out of a string

Raja Mirrah
Greenhorn

Joined: Jun 23, 2009
Posts: 26


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.


    Your early response is highly appreciated.

    Regards,

    Raj.


    Warm Regards,
    Raj
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    freakin raja please check your private messages.


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    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:


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Raja Mirrah
    Greenhorn

    Joined: Jun 23, 2009
    Posts: 26
    Thank you paul for your reply

    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

    70854799525 packets input, 19151340856378 bytes
    73032762307 packets output, 34009367972443 bytes



    But sometimes the strings may come in one single line like this

    70854799525 packets input, 19151340856378 bytes 73032762307 packets output

    The regex which i wrote is not matching this type of input.

    So i need a regex which will be used in both the scenarios.


    Warm Regards,

    Raj,
    D. Ogranos
    Ranch Hand

    Joined: Feb 02, 2009
    Posts: 214
    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.
    Raja Mirrah
    Greenhorn

    Joined: Jun 23, 2009
    Posts: 26
    Hi Ogranos,

    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.


    Warm Regards,

    Raj
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    Raj developer, please check your private messages again.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    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)

    For more info see java.util.regex.Pattern.
    Raja Mirrah
    Greenhorn

    Joined: Jun 23, 2009
    Posts: 26
    i tried your code but it still gave only integer before packets output


    look at the code attached below with your regex and its output








    Output is


    input----->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#
    73032762307 packets output
    73032762307
    output

    m.group(0)73032762307 packets output
    m.group(1)73032762307
    m.group(2)output


    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.


    Warm Regards,

    Raj
    Raja Mirrah
    Greenhorn

    Joined: Jun 23, 2009
    Posts: 26
    i tried your code but it still gave only integer before packets output


    look at the code attached below with your regex and its output








    Output is


    input----->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#
    73032762307 packets output
    73032762307
    output

    m.group(0)73032762307 packets output
    m.group(1)73032762307
    m.group(2)output


    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.


    Warm Regards,

    Raj
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18509
        
      40

    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Raja Mirrah
    Greenhorn

    Joined: Jun 23, 2009
    Posts: 26
    Thank you all .

    It did work .

    I just did a slight modification in the regex which you gave.....


    (\\d+) packets\\s*((input|output))


    It works now......

    thank you all once again......

    Warm Regards,

    Raj
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Regular expression to take integers out of a string
     
    Similar Threads
    Creating an editable table using an array of input tags
    Reading your Data File
    Soo close Part II
    XML mapping rules
    Input in Java