0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

This is just what I need! But is there a simpler pattern?
Is the 6 considered a word character? I don't see a integer regex.
yep, it;'s a string. [^\\w] matches any character but not of a word character that is A-Z,a-z,0-9,_ . It really needs replacing and then splitting. I think there is no shorter regex than this.
This is just the same without double slashes. Honestly, I don't understand regex escapes in any language. I know I've seen you around in Python a lot, and I have difficulty differentiating regex as a raw string as opposed to a literal string.
@MalikBrahimi you need to escape the backslash one more time in java. That's all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.