This first removes all non-letter characters, folds to lowercase, then splits the input, doing all the work in a single line:
String[] words = instring.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
Spaces are initially left in the input so the split will still work.
By removing the rubbish characters before splitting, you avoid having to loop through the elements.
Related Posts:
- How can I remove punctuation from input text in Java?
- How to replace ” \ ” with ” \\ ” in java
- Using or ‘|’ in regex [duplicate]
- How can I exclude one word with grep?
- Change date format in a Java string
- Split string on whitespace in Python
- Regex how to match an optional character
- What does regular expression \\s*,\\s* do?
- Java; String replace (using regular expressions)?
- Regular Expressions on Punctuation
- How to extract a substring using regex
- Regular expression – starting and ending with a character string
- How to use regex in String.contains() method in Java
- Regex empty string or email
- Java – Convert integer to string [duplicate]
- What is a Context Free Grammar?
- How to convert/parse from String to char in java?
- Java String Split by “|”
- How can I validate an email address using a regular expression?
- Regular expression to match a line that doesn’t contain a word
- Difference between \b and \B in regex
- Java string to date conversion
- Python regex AttributeError: ‘NoneType’ object has no attribute ‘group’
- Regular Expression for alphanumeric and underscores
- Regular Expression for alphanumeric and underscores
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- Regex to test if string begins with http:// or https://
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- Best way to convert an ArrayList to a string
- String interpolation in Java 14 or 15
- What is a non-capturing group in regular expressions?
- What is a non-capturing group in regular expressions?
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- How to convert any Object to String?
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Symbol for any number of any characters in regex?
- Grep and Python
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Can I multiply strings in Java to repeat sequences?
- Regular expression to match a word or its prefix
- catDog string problem at Codingbat.com
- What is the difference between String.subString() and String.subSequence()
- Java compressing Strings
- Question marks in regular expressions
- Convert array of strings into a string in Java
- Difference between \w and \b regular expression meta characters
- How to print elements from array with javascript
- How to remove single character from a String
- javascript HTML from document.body.innerHTML
- How can I write a regex which matches non greedy?
- Java split string to array
- Regex Explanation ^.*$ [duplicate]
- Negative matching using grep (match lines that do not contain foo)
- What does {0} mean when found in a string in C#?
- Regex match everything after question mark?
- How to check if my string is equal to null?
- what is Ljava.lang.String;@
- Converting String Array to an Integer Array
- How is \\n and \\\n interpreted by the expanded regular expression?
- How to sort an array of objects in Java?
- Regular Expression to reformat a US phone number in Javascript
- How to replace a substring of a string
- Immutable class?
- RegEx to find two or more consecutive chars
- RE error: illegal byte sequence on Mac OS X
- Regex to match an optional ‘+’ symbol followed by any number of digits
- Regex Match all characters between two strings
- Converting a string to an integer on Android
- Regular expression to match a word or its prefix
- Java: how to initialize String[]?
- Regular expression for a string that does not start with a sequence
- How to represent a fix number of repeats in regular expression?
- python regular expression “\1”
- Regular expression to stop at first match
- How can I remove a substring from a given String?
- Java get String CompareTo as a comparator object
- Whitespace Matching Regex – Java
- /bb|[^b]{2}/ how does it work?
- How to swap String characters in Java?
- Get an OutputStream into a String
- Difference between string object and string literal
- How do I match any character across multiple lines in a regular expression?
- Regular expression to remove HTML tags from a string
- Regular expression to limit number of characters to 10
- My Vim replace with a regex is throwing a `E488: Trailing characters`
- Append a single character to a string or char array in java?
- String is immutable. What exactly is the meaning?
- Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
- Regex difference: (\w+)? and (\w*)
- Java regex email
- How to know if a given string is substring from another string in Java
- 404 regular-expression advice needed
- How to use a variable as a className inside preg_match_all parameters?
- Adding span tags to post titles using regex
- Remove Custom HTML Posts Using Regex
- Regexes allow only [a-z][A-Z][0-9] and –
- Call Shortcode Attribute Value within another function
- Use Regex Redirection via htsaccess for RSS Feeds WordPress