• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression to take integers out of a string

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
     
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    freakin raja please check your private messages.
     
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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:
     
    Raja Mirrah
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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,
     
    Ranch Hand
    Posts: 214
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Raj developer, please check your private messages again.
     
    Rob Spoor
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Raja Mirrah
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic