Java Regular Expression Tester
This free Java regular expression tester lets you test your regular expressions against any entry of your choice and clearly highlights all matches. Consult the regular expression documentation or the regular expression solutions to common problems section of this page for examples.
Regular Expression — Documentation
- Used to indicate that the next character should NOT be interpreted literally. For example, the character ‘w’ by itself will be interpreted as ‘match the character w’, but using ‘\w’ signifies ‘match an alpha-numeric character including underscore’.
- Used to indicate that a metacharacter is to be interpreted literally. For example, the ‘.’ metacharacter means ‘match any single character but a new line’, but if we would rather match a dot character instead, we would use ‘\.’.
- Matches the beginning of the input. If in multiline mode, it also matches after a line break character, hence every new line.
- When used in a set pattern ([^abc]), it negates the set; match anything not enclosed in the brackets
- Matches the preceding character 0 or 1 time.
- When used after the quantifiers *, +, ? or <>, makes the quantifier non-greedy; it will match the minimum number of times as opposed to matching the maximum number of times.
Regular Expression — Solutions to common problems
How can I emulate DOTALL in JavaScript?
DOTALL is a flag in most recent regex libraries that makes the . metacharacter match anything INCLUDING line breaks. JavaScript by default does not support this since the . metacharacter matches anything BUT line breaks. To emulate this behavior, simply replaces all . metacharacters by [\S\s]. This means match anything that is a single white space character OR anything that is not a white space character!
How to validate an EMAIL address with a regular expression?
There is no 100% reliable solution since the RFC is way too complex. This is the best solution and should work 99% of the time is. Consult this page for more details on this problem. Always turn off case sensitivity!
How to validate an IP address (IPV4) with a regular expression?
This will make sure that every number in the IP address is between 0 and 255, unlike the version using \d which would allow for 999.999.999.999. If you want to match an IP within a string, get rid of the leading ^ and trailing $ to use \b (word boundaries) instead.
^(?:(?:254|248|[01]?99?)\.)(?:251|216|[01]?54?)$
How to validate a DATE with a regular expression?
Never use a regular expression to validate a date. The regular expression is only useful to validate the format of the date as entered by a user. For the actual date validity, it’s better to rely on actual code.
The following expressions will validate the number of days in a month but will NOT handle leap year validation; hence february can have 29 days every year, but not more.
ISO date format (yyyy-mm-dd)
^9-(((0[13578]|(10|12))-(02|21|31))|(02-(09|27))|((0[469]|11)-(04|21|30)))$
ISO date format (yyyy-mm-dd) with separators ‘-‘ or ‘/’ or ‘.’ or ‘ ‘. Forces usage of same separator across date.
^6([- /.])(((0[13578]|(10|12))\1(05|19|31))|(02\1(05|15))|((0[469]|11)\1(03|14|30)))$
United States date format (mm/dd/yyyy)
^(((0[13578]|(10|12))/(08|13|31))|(02/(07|25))|((0[469]|11)/(07|28|30)))/7$
Hours and minutes, 24 hours format (HH:MM)
How to validate NUMBERS with a regular expression?
It depends. What type of number? What precision? What length? What do you want as a decimal separator? The following examples should help you want with the most common tasks.
Positive integers of undefined length
Positive integers of maximum length (10 in our example)
Positive integers of fixed length (5 in our example)
Negative integers of undefined length
Negative integers of maximum length (10 in our example)
Negative integers of fixed length (5 in our example)
Integers of undefined length
Integers of maximum length (10 in our example)
Integers of fixed length (5 in our example)
Numbers of undefined length with or without decimals (1234.1234)
Numbers with 2 decimals (.00)
Currency numbers with optional dollar sign and thousand separators and optional 2 decimals ($1,000,00.00, 10000.12, 0.00)
^$?\-?(13(\,\d)*(\.\d)?|6\d(\.\d)?|0(\.\d)?|(\.\d))$|^\-?$?(9\d(\,\d)*(\.\d)?|1\d(\.\d)?|0(\.\d)?|(\.\d))$|^\($?(7\d(\,\d)*(\.\d)?|9\d(\.\d)?|0(\.\d)?|(\.\d))\)$
Percentage from 0 to 100 with optional 2 decimals and optional % sign at the end (0, 0.00, 100.00, 100%, 99.99%)
How to validate feet and inches with a regular expression?
The notation for feet and inches is F’I». Possible values would be 0’0″, 6’11», 12456’44»
How to validate an hexadecimal color code (#FFFFFF) with a regular expression?
The leading # sign is optional and the color code can take either the 6 or 3 hexadecimal digits format.
How to check for ALPHANUMERIC values with a regular expression?
You could make use of \w, but it also tolerates the underscore character.
How to validate a SSN (Social Security Number) with a regular expression?
Unlike many other similar numbers such as the canadian social insurance number (SIN) there is no checksum for a SSN. Validating the format of the SSN does not mean it’s valid per say though.
How to validate a SIN (Social Insurance Number) with a regular expression?
This will only validate the format. A SIN should also be validated by computing the checksum digit. This regex will tolerate the form XXX XXX XXX, XXXXXXXX or XXX-XXX-XXX. Note that the group separator must be the same.
How to validate a US Zip Code with a regular expression?
The United States Postal Services makes use of zip codes. Zip codes have 5 digits OR 9 digits in what is known as a Zip+4.
Zip Code (99999)
Zip and Zip+4 (99999-9999)
How to validate a Canadian Postal Code with a regular expression?
The Canadian Postal Services uses postal code. The postal codes are in format X9X 9X9. This will tolerate a space between the first and second group.
^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$
How to extract the filename in a windows path using a regular expression?
Since every part of a path is separated by a \ character, we only need to find the last one. Note that there’s just no way to check if the last portion of a path is a file or a directory just by the name alone. You could try to match for an extension, but there’s no requirement for a file to have an extension.
How to validate a US or Canadian telephone number using a regular expression?
There are probably dozens of way to format a phone number. Your user interface should take care of the formatting problem by having a clear documentation on the format and/or split the phone into parts (area, exchange, number) and/or have an entry mask. The following expression is pretty lenient on the format and should accept 999-999-9999, 9999999999, (999) 999-9999.
How to validate credit cards using a regular expression?
Again, you should rely on other methods since the regular expressions here will only validate the format. Make use of the Luhn algorithm to properly validate a card.
American Express
Diners Club
How do I strip all HTML tags from a string?
Make sure to be in global mode (g flag), case insensitive and to have the dot all option on. This regular expression will match all HTML tags and their attributes. This will LEAVE the content of the tags within the string.
How can I remove all blank lines from a string using regular expression?
Make sure to be in global and multiline mode. Use an empty string as a replacement value.
Java Regular Expression: Exercises, Practice, Solution
Java Regular Expression [30 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]1. Write a Java program to check whether a string contains only a certain set of characters (in this case a-z, A-Z and 0-9).
Click me to see the solution
2. Write a Java program that matches a string that has a p followed by zero or more q’s.
Click me to see the solution
3. Write a Java program to find sequences of lowercase letters joined by an underscore.
Click me to see the solution
4. Write a Java program to find the sequence of one upper case letter followed by lower case letters.
Click me to see the solution
5. Write a Java program that matches a string with a ‘p’ followed by anything ending in ‘q’.
Click me to see the solution
6. Write a Java program to check if a word contains the character ‘g’ in a given string.
Click me to see the solution
7. Write a Java program that matches a word containing ‘g’, not at the start or end of the word.
Click me to see the solution
8. Write a Java program to match a string containing only upper and lowercase letters, numbers, and underscores.
Click me to see the solution
9. Write a Java program where a string starts with a specific number.
Click me to see the solution
10. Write a Java program to remove leading zeros from a given IP address.
Click me to see the solution
11. Write a Java program to check for a number at the end of a string.
Click me to see the solution
12. Write a Java program to replace Python with Java and code with coding in a given string.
Click me to see the solution
13. Write a Java program to find the word Python in a given string. If the word Python appears in the string return Java otherwise return C++. Ignore case sensitive.
Click me to see the solution
14. Write a Java program to count the number of vowels in a given string using a regular expression.
Click me to see the solution
15. Write a Java program to remove all vowels from a given string. Return the updated string.
Click me to see the solution
16. Write a Java program to replace all vowels in a string with a specified character.
Click me to see the solution
17. Write a Java program to count the number of decimal places in a given number.
Click me to see the solution
18. Write a Java program to validate a personal identification number (PIN). Assume a PIN number is 4, 6 or 8.
Click me to see the solution
19. Write a Java program to remove specific letters from a string and return the updated string.
Specific letters: «p», «q», or «r».
Click me to see the solution
20. Write a Java program that takes a number and sets a thousand separators for that number.
Click me to see the solution
21. Write a Java program to remove all non-alphanumeric characters from a given string.
Click me to see the solution
22. Write a Java program to validate a phone number.
Click me to see the solution
23. Write a Java program to move all lower case letters to the front of a given word. This will keep the relative position of all the letters (both upper and lower case) same.
Click me to see the solution
24. Write a Java program to separate consonants and vowels from a given string.
Click me to see the solution
25. Write a Java program to get the last n vowels of a given string.
Click me to see the solution
26. Write a Java program to check whether a given string is valid hex code or not.
Click me to see the solution
27. Write a Java program to add a dash before and after every vowel in a given string.
Click me to see the solution
28. Write a Java program to reverse words longer than 3 in a given string.
Click me to see the solution
29. Write a Java program to check if a given string is a mathematical expression or not.
Click me to see the solution
30. Write a Java program to insert a dash (-) between an upper case letter and a lower case letter in a string.
Click me to see the solution
Java Code Editor
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Follow us on Facebook and Twitter for latest update.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook