Java - 正则表达式分割输入文本但保留分隔符

kin*_*tik 0 java regex

寻找一些正则表达式的帮助.我正在寻找一种Java方法,用文字分割一些输入文本,但也保留分隔符(空格,标点符号).另一种方法是将单词拆分为自己的索引,其他非单词字符可以放在数组的其他索引中.

这个输入文字:

"Hello, this isn't working!"
Run Code Online (Sandbox Code Playgroud)

应该放入这样的数组:

{"Hello", ",", "this", "isn't", "working", "!"}
Run Code Online (Sandbox Code Playgroud)

要么

{"Hello", ", ", "this", " ", "isn't", " ", "working", "!"}
Run Code Online (Sandbox Code Playgroud)

我用Python做了基本相同的事情:

def split_input(string):
    return re.findall(r"[\w']+|[\s.,!?;:-]", string)
Run Code Online (Sandbox Code Playgroud)

但我还没有找到一种在Java中完成同样事情的方法.我尝试String.split()过前瞻/后视,我尝试过模式匹配,但运气不好.

任何帮助将非常感激!

Bri*_*ham 5

split不是Python的Java模拟findall.Matcher.find是.

Pattern stuff = Pattern.compile("[\\w']+|[\\s.,!?;:-]");
Matcher matcher = stuff.matcher("Hello, this isn't working!");
List<String> matchList = new ArrayList<String>();
while (matcher.find()) {
    matchList.add(matcher.group(0)); // add match to the list
}
Run Code Online (Sandbox Code Playgroud)