Match a word using regex that also handles apostrophes

前端 未结 2 1227
[愿得一人]
[愿得一人] 2021-01-13 18:34

I have to separate a line of text into words, and am confused on what regex to use. I have looked everywhere for a regex that matches a word and found ones similar to this

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 19:22

    The following regex seems to cover your sample string correctly. But it doesn't cover you scenario for the apostrophe.

    [\s,.?!"]+
    

    Java Code:

    String input = "I like to eat but I don't like to eat everyone's food, or they'll starve.";
    String[] inputWords = input.split("[\\s,.?!]+");
    

    If I understand correctly, the apostrophe should be left alone as long as it is after a word character. This next regex should cover the above plus the special case for the apostrophe.

    (?

    Java Code:

    String input = "I like to eat but I don't like to eat everyone's food, or they'll starve.";
    String[] inputWords = input.split("(?

    If I run the second regex on the string: Hey there! Don't eat 'the mystery meat'. I get the following words in my string array:

    Hey
    there
    Don't
    eat
    the
    mystery
    meat'
    

提交回复
热议问题