Link to home
Start Free TrialLog in
Avatar of DTwined
DTwined

asked on

Parse String

Hello -
I'm looking for a good and efficient way to parse the following string. I want to loop and pull all the numbers out of the string. Please note there is a space before and after the number.

"AAA: 111 BBB: 222 CCC: 333 DDD: 444 EEE: 555"

thanks!
DT
Avatar of for_yan
for_yan
Flag of United States of America image

  String ssline =   "AAA: 111 BBB: 222 CCC: 333 DDD: 444 EEE: 555";

        String [] sslines = ssline.trim().split("[A-Za-z\\s:]+");

        for(int j=1; j<sslines.length; j++){


            System.out.println( sslines[j]);
        }

Open in new window



output:

111
222
333
444
555

Open in new window

Avatar of DTwined
DTwined

ASKER

Looks good and you get the points but I screwed up and left one thing out.

What if one of the values I'm trying to pull has a comma, is there a way I can ignore it.

For example:

String ssline =   "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"

I need to keep "Smith, Bill" together.

thanks!
commaa is not a problem,

see below:

      String ssline =   "AAA: 111 BBB: 222 CCC: 333 D,DD: 444 EEE: 555";

        String [] sslines = ssline.split("[\\D,]+");

        for(int j=1; j<sslines.length; j++){


            System.out.println( "ss" + sslines[j]);
        }

Open in new window



ss111
ss222
ss333
ss444
ss555



WAht is a problem is that I acnnot get away from the first empty line - i just ignored it startig form index 1
but if it starts from the number you need  not to ignore it
Thsi would work for all lines
if they start from number or not
 String ssline =   "AAA: 111 BBB: 222 CCC: 333 D,DD: 444 EEE: 555";

        String [] sslines = ssline.split("[\\D,]+");

        for(int j=0; j<sslines.length; j++){
            if(j==0 && sslines[j].trim().length() == 0)continue;


            System.out.println(sslines[j]);
        }

Open in new window

Output:
111
222
333
444
555

Open in new window


but still I'm unhappy
Avatar of DTwined

ASKER

Thanks. It's still not doing exactly what I need. My fault, I should have not just done all numbers. Could you get it to parse my example string. There may be cases where there are alphabetic chars after the colon.

String ssline =   "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555"

I'd like to get back the following:
111
Smith, Bill
333
444
555

thanks!
tehn formulate - what do you want to remove?
what is the dofference between say "BBB"  and "Smith" ?
We can split by colon, but how do we know what to remove -
llike say all words ending with colon?
Avatar of DTwined

ASKER

Yeah, I need to pull the value after each colon.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DTwined

ASKER

Thanks!
This would be still better, as it dowes not ignore first empty string,
but rather splits withourt it

    String ssline =   "AAA: 111 BBB: Smith, Bill CCC: 333 DDD: 444 EEE: 555";

        String [] sslines = ssline.replaceFirst("^[A-Z]+:","").split("[A-Z]+:");

        for(int j=0; j<sslines.length; j++){
           


            System.out.println(sslines[j].trim());
        }

Open in new window


Output:
111
Smith, Bill
333
444
555

Open in new window

Avatar of DTwined

ASKER

Thanks. One more question what if the letters before the colon were a mix of Upper and Lower case?

for example:


"Aaa: 111 Bbb: Smith, Bill Ccc: 333 Ddd: 444 Eee: 555";

thanks!
    String ssline =   "AAA: 111 Bbb: Smith, Bill CCC: 333 DDD: 444 EEE: 555";

        String [] sslines = ssline.replaceFirst("^[A-Za-z]+:","").split("[A-Za-z]+:");

        for(int j=0; j<sslines.length; j++){
           


            System.out.println(sslines[j].trim());
        }

Open in new window


111
Smith, Bill
333
444
555

Open in new window

Avatar of DTwined

ASKER

Awesome! Works great!