I'm returning to Java after a several-year hiatus with Ruby. I'm looking for idiomatic and short Java code that accomplishes the following Ruby statement:
some_string.scan(/[\w|\']+/)
The above expression creates an array from a string. The elements in the array are all the sections of some_string that are composed of either alphanum chars (\w) or the apostrophe (\' so that "John's" is not split into two words.)
For example:
"(The farmer's daughter) went to the market".scan(/[\w|\']+/)
=>
["The", "farmer's", "daughter", ...]
Update
I know the solution will use something like this:
String[] words = sentence.split(" ");
I just need the regex part that goes in split().
|in a character class (surrounded by brackets[ ]), and you don't need to escape the'. The regular expression/[\w']+/is correct, and yours is buggy.