Reputation: 16721
I wish to take a string input from the user and extract words or numbers like so:
String problem = "I'm lo#o@king t%o ext!r$act a^ll 6 su*bs(tr]i{ngs.";
String[] solve = {"I'm", "looking", "to", "extract", "all", "6", "substrings"};
Basically, I want to extract numbers and words with complete disregard to punctuation except apostrophes. I know how to get words and strings but I can't seem to figure out this tricky part.
Upvotes: 0
Views: 65
Reputation: 174836
You could do like the below.
String s = "I'm lo#o@king t%o ext!r$act a^ll 6 su*bs(tr]i{ngs.";
String parts[] = s.replaceAll("[^\\s\\w']|(?<!\\b)'|'(?!\\b)", "").split("\\s+");
System.out.println(Arrays.toString(parts));
Output:
[I'm, looking, to, extract, all, 6, substrings]
Explanation:
[^\\s\\w']
matches any character but not of space or single quote or word character.
(?<!\\b)'(?!\\b)
matches the '
symbol only if it's not preceded and not followed by a word character.
replaceAll
function replaces all the matched characters with an empty string.
Finally we do splitting on the resultant string according to one or more space characters.
Upvotes: 1