1

I have a java string 'str'

String str= " 1p   qrn qr2 qst   1a1 4at   qa qb1  st1 sp su";

I want to split it to get the following 'parts' array

String [] parts={"1p","qrn qr2 qst" ,"1a1 4at" ,"qa qb1" ,"st1 sp su"};

Elements of 'parts' start with char 0-9, a,q,s.

Help me for a suitabe regex such that:

String [] parts= str.split(a suitable regex);

or any other alternative which can do it.

4
  • 1
    "qrn qr2 qst" is supposed to be one part? According to your spec it should be 3, no? - Ahh... got it. Never mind Commented Jun 5, 2014 at 7:31
  • Yes, because each word starts with q. Commented Jun 5, 2014 at 7:35
  • so it should be String [] parts={"1p","qrn", "qr2", "qst" ,"1a1","4at" ,"qa", "qb1" ,"st1", "sp", "su"}; Commented Jun 5, 2014 at 7:37
  • FYI, added Java code online demo to my answer. :) Commented Jun 5, 2014 at 7:42

3 Answers 3

5

This regex should work for you:

[\daqs][\w ]*?(?=\s{2}|$)

Working Demo

For splitting use this regex:

\s{2,}(?=[\daqs])

Working Demo 2

Java Code:

String [] parts = str.split("\\s{2,}(?=[\\daqs])");
System.out.println(Arrays.toString(parts)); 
Sign up to request clarification or add additional context in comments.

8 Comments

The output I am getting is:
The output I am getting is:[ , , , , ] The code is used as: String [] parts= str.split("[\\daqs][\\w ]*?(?=\\s{2}|$)"); System.out.println(Arrays.toString(parts)); There is some flaw, kindly help to correct it.
System.out.println( parts.length); Gives 1, when it should be more than 1, kindly confirm the matter.
Sad that even after providing you fully working code you cannot adapt it. See this working demo: ideone.com/OpHYat
After resetting the java and netbean on my computer your code has worked well on my computer. Thanks great man.
|
1

With your string, this will work:

String[] yourArray = subjectString.split("^\\s+|\\s{2,}");

See the output at the bottom of the online demo.

How does this work?

It looks like your tokens are separated by multiple places. This is what we will use to split: \s{2,} In addition, we want to remove any spaces at the beginning of the string, so we add this to the split conditions: ^\s+

Comments

0

I wish I could comment instead of answer but unfortunately I can't so I have to just do an answer. What you said doesn't actually work - as if it started with those characters, it would end up as

String [] parts={"1p", "qrn", "qr2", "qst" ,"1a1", "4at" ,"qa", "qb1" ,"st1", "sp", "su"};

so to answer your actual question,

[0-9aqs]\w+

should work as you want. If that's not what you want, clarify how they are split, because how you split doesn't fit "Elements of 'parts' start with char 0-9, a,q,s."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.