Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:33:48+00:00 2026-05-16T01:33:48+00:00

I’m a complete noob to regex and I need help with splitting a string.

  • 0

I’m a complete noob to regex and I need help with splitting a string. I am inputing the following data

665  11% R     1    908K    388K  fg root     top
 61   1% S    42 152404K  29716K  fg system   system_server
 38   0% S     1    840K    340K  fg root     /system/bin/qemud
114   0% S    16 120160K  19156K  fg radio    com.android.phone

which is nothing but your regular top output. What I intend to do is select on entries like

655 11% R 1 fg root top

Now the code which I use to do the following is

while ((inputLine = in.readLine()) != null) 
{
  String[] segs= inputLine.split("[ ]+");
  str[i] = segs[0]+" "+segs[1]+" "+segs[2]+" "+
           segs[3]+" "+segs[6]+" "+segs[7]+" "+segs[8];
  Log.v("TOP Output", str[i]);
  i++; j++;
}

But the problem I face is, that I get on logcat is

java.lang.ArrayIndexOutOfBoundsException

Where am I going wrong, and what could I do different to prevent this. Thanks for helping.

EDIT: After reading the comments I realize i have a couple of empty line in my output. So in such a case how am I supposed to ignore those line. I know I am supposed to match a case, but I am not sure about the expression or syntax!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:33:49+00:00Added an answer on May 16, 2026 at 1:33 am

    You don’t need the character class (square brackets). Space is a regular character in regex, so:

    String[] segs = inputLine.split(" +");
    

    Other than that, assuming array indices are there without range checking is bad style and an ArrayIndexOutOfBoundsException is just what you’ve asked for.

    Better do it explicitly:

    String re = "^\\s*(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\s+(\\S+)\\s+(\\S+)\\s*$";
    Pattern p = Pattern.compile(re, Pattern.MULTILINE);
    Matcher m = p.matcher(yourInputString);
    
    while (m.find())
    {
       // do stuff with m.group(1) through m.group(9)
    }
    

    This way it is guaranteed that every line you match fulfills your expectations and every matcher group contains what you expect, too.

    Disclaimer: I’m not especially proud of that regex. It’s quite an ugly one, actually, but it illustrates the point that explicit is more reliable and predictable than implicit. And it has the potential to be improved into a version that matches the desired parts even more accurately than a string split ever could.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.