lahiru madhumal
lahiru madhumal

Reputation: 1201

Java tokenize a string

I would like to tokenize the following string. "I went home with Mark's brother, to play a video game." result should be like this.

I
went
home
with
Mark
'
s
brother
to
,
play
a
video
game
.

Can you please tell me how to do it using regex.

Upvotes: 2

Views: 218

Answers (1)

Keppil
Keppil

Reputation: 46239

If you really want the ,, . and ' as separate tokens, you could split like this:

String str = "I went home with Mark's brother, to play a video game.";
String[] tokens = str.split("(\\s|(?=[,.'])|(?<=[,.']))");

Upvotes: 2

Related Questions