10 Example of Regular Expression in Java
Here are the 10 common example of using Regular expression in Java. You can us these regex commands or patterns to match text against common scenarios1. Exact Search
"java" matches any string that has the text "java" in it like "javarevisited"
2. Startswith
^Test matches any string that starts with "Test" like "Testing"
3. EndsWith
ing$ matches a string that ends with "ing" like playing, reading, watching, doing etc
4. In Between
^The end$ exact string match (starts and ends with The end)
4. Quantifiers?—?* + ? and {}
abc* matches a string that has ab followed by zero or more c -> Try it!
abc+ matches a string that has ab followed by one or more c
abc? matches a string that has ab followed by zero or one c
abc{2} matches a string that has ab followed by 2 c
abc{2,} matches a string that has ab followed by 2 or more c
abc{2,5} matches a string that has ab followed by 2 up to 5 c
a(bc)* matches a string that has a followed by zero or more copies of the sequence bc
a(bc){2,5} matches a string that has a followed by 2 up to 5 copies of the sequence bc
OR operator?—?| or []
a(b|c) matches a string that has a followed by b or c -> Try it!
a[bc] same as previous
5. Character classes?—?\d \w \s and .
\d matches a single character that is a digit -> Try it!
\w matches a word character (alphanumeric character plus underscore) -> Try it!
\s matches a whitespace character (includes tabs and line breaks)
. matches any character -> Try it!
6. Use the . operator carefully since often class or negated character class (which we’ll cover next) are faster and more precise.
7. \d, \w and \s also present their negations with \D, \W and \S respectively. For example, \D will perform the inverse match with respect to that obtained with \d. \D matches a single non-digit character
Some important things to Remember
1. In order to be taken literally, you must escape the characters ^.[$()|*+?{\with a backslash \ as they have special meaning.
\$\d matches a string that has a $ before one digit
2. Notice that you can match also non-printable characters like tabs \t, new-lines \n, carriage returns \r.
3. The quantifiers ( * + {}) are greedy operators, so they expand the match as far as they can through the provided text
That's all about 10 essential Regular expressions every Java Programmer should know. I have purposefully not touched some advanced topics like grouping and back reference just to keep this article simple but if you are interested you can also check a comprehensive Regular expression resource like RegEx MasterClass to learn it better.
Other Java Regular Expression tutorials you may like:
- How to split comma separated String in Java?
- How to check if a given String is number in Java using regular expression?
- How to split a string by whitespace in Java?
- How to check if a string is numeric?
- How to remove all special characters from a String in Java?
- 7 Best Courses to learn Regular Expression in depth
Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.