How to split a string on whitespace and on special char while getting there offset values in java

Viewed 85

I am try to split/match a string on Punctuation and white space and also need to get there offset values.

Ex - "I live, in India."

i want output like - ["I","live", ",", "in", "India", "."] and also the start and end index value of each token.

I have tried using -

String text = "I live, in India.";

Pattern p1 = Pattern.compile("\\S+");
Pattern p2 = Pattern.compile("\\p{Punct}");         
Matcher m1 = p1.matcher(text);
Matcher m2 = p2.matcher(text);

This will give the desire result but can i combine both the pattern in a single pattern ?

1 Answers

Matcher#start

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\b\\S+\\b|\\p{Punct}");
        Matcher matcher = pattern.matcher("I live, in India.");
        while (matcher.find()) {
            System.out.println(matcher.group() + " => " + matcher.start());
        }
    }
}

Output:

I => 0
live => 2
, => 6
in => 8
India => 11
. => 16

Explanation of regex:

  1. \b specifies word boundary.
  2. | specifies OR.
  3. \p{Punct} specifies punctuation.
  4. \S+ specifies one or more non-whitespace character.
Related