0

I'm after some help with a regex that I can't get to work correctly, I've used a few online tools to test the patterns but with little success.

I need to split a string based on a pattern FS[0-9][0-9], but also include some trailing text which could be any length comma separated text and numbers.

For example: FS01,a,b,c,d,1,2,3FS02,x,y,zFS03,some random text,123FS04,1

Would need to be split into:

  • FS01,a,b,c,d,1,2,3
  • FS02,x,y,z
  • FS03,some random text,123
  • FS04,1
3
  • So, basically, you want FSFollowedBySomething Commented Aug 6, 2014 at 9:44
  • FSnumbernumber, followed by something Commented Aug 6, 2014 at 9:45
  • Oh.. then you could check my answer. Commented Aug 6, 2014 at 9:52

3 Answers 3

3

Use a negative lookbehind and positive lookahead to get the splits.

String s = "FS01,a,b,c,d,1,2,3FS02,x,y,zFS03,some random text,123FS04,1";
String tok[] = s.split("(?<!^)(?=FS\\d{2})");
System.out.println(tok[0]);
System.out.println(tok[1]);
System.out.println(tok[2]);
System.out.println(tok[3]);

Output:

FS01,a,b,c,d,1,2,3
FS02,x,y,z
FS03,some random text,123
FS04,1

DEMO

Explanation:

  • (?<!^) Negative lookbehind asserts that what preceding is not the start of the line.
  • (?=FS\\d{2}) Lookahead asserts that what following is FS followed by two digits. So it sets the matching marker just before to all the FS\d\d but not the one at the start.
Sign up to request clarification or add additional context in comments.

2 Comments

I'd replace (?=FS) with (?=FS\d{2}) to match the OP's requirements. And maybe you want to explain what the code does, for others that read this answer.
You can change your println() lines to for (String token : tok) System.out.println(token); for simpler and readable code. :)
2

Try this REGEX :

public static void main(String[] args) {
    String s = "FS01,a,b,c,d,1,2,3FS02,x,y,zFS03,some random text,123FS04,1";
    Pattern p = Pattern.compile("(FS.*?)(?=(FS|$))");
    // positive Lookahead. Captures groups starting with FS and ending upto another FS or end of String (denoted by $)
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
    }
}

O/P :

FS01,a,b,c,d,1,2,3
FS02,x,y,z
FS03,some random text,123
FS04,1

2 Comments

Changed the pattern to ((FS\d{2}).*?)(?=(FS|$)) and is exactly what im wanting thanks
@MattRoberts - Glad i could be of help :)
0

Try this regex here FS.*?(?=FS)

http://www.regexr.com/3999u

1 Comment

doesn't match the last FS. i.e, FS04,1

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.