- Saved searches
- Use saved searches to filter your results more quickly
- nickawatts/regex-tester
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Java Regular Expression Tester
- Regular Expression — Documentation
- Regular Expression — Solutions to common problems
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Java library that helps you test regular expressions with JUnit.
nickawatts/regex-tester
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A Java library that reduces the complexity of testing regular expressions with JUnit.
If you need support for earlier Java or JUnit versions, please submit an issue report explaining your needs.
regex-tester provides a simple API for testing regular expressions in Java. Using annotations that fit naturally with JUnit, you simply specify the regular expression, the strings to test against and indicate if each string should produce a match. In other words, regex-tester handles the boiler-plate code that you must write to perform such tests in JUnit and leaves only a simple declaration of what is to be tested.
Why Have a Regex Test Library?
Because regular expressions are complex and error prone and you probably don’t unit test them! A regular expression, or regex for short, often represents important business logic and can expand out to thousands, or more, variations. Since regexes are so compact and ubiquitous in programming, developers tend to overlook them when looking for code to test. And since code coverage tools overlook them too, every regex represents complex, brittle yet important code that is not covered by unit tests.
regex-tester provides a fluent API that hooks into JUnit. With this API, you can create a JUnit test case for each regex you want to test. The test case below shows several assertions being made about strings that match and don’t match the regex The\s+Journal\s+of\s+(\w+) . You can make simple assertions like «The string ‘blah’ does match the regex but the string ‘woohoo’ doesn’t». You can also assert that numbered capture groups in the regex do or don’t match specific text strings. The fluent API allows you to build up a set of assertions to make against the regex, all of which are automatically executed when you run the unit test.
@RunWith(value=RegexTestSuite.class) @Regex(value="The\\s+Journal\\s+of\\s+(\\w+)") public class BasicRegexTest < @RegexAssertions public static RegexAssertionSet getRegexAssertions() < /* * A public static method named getRegexAssertions and annotated * with @RegexAssertions defines the things to assert about the regex. */ return new RegexAssertionSetBuilder() .addMatchAssertion("The Journal of Physics") .addNoMatchAssertion("The Journal of Physics Letters") .addMatchAssertion("The Journal of Chemistry") .addGroupMatchesAtAssertion(1, "Physics", "The Journal of Physics", true) .addGroupMatchesAtAssertion(1, "Physics", "The Journal of Chemistry", false) .build(); >@Test public void test() < /* * A single, empty test method must exist to satisfy JUnit. */ >>
regex-tester uses as little boiler-plate code as possible so that each test case makes a clear statement about various types of strings that match and don’t match the regex. The @RunWith annotation delegates responsibility for running the test to the API. @Regex decleares the regex that is under test. @RegexAssertions declares the assertions you want to make about what does and doesn’t match the regex. Using just three annotations, you’re able to communicate your expectations about the usage and limits of a regex to other developers.
The test suite for the API itself is meant to demonstrate its capabilities. There are many tests and they’re commented to help you figure out how to use the library. Start with the BasicRegextTest class. It shows the most common usage scenarios for the libray, just like in the Basic Usage section above.
com.thewonggei regex-tester 0.2
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|217|[01]?92?)\.)(?:251|224|[01]?29?)$
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)
^7-(((0[13578]|(10|12))-(05|26|31))|(02-(07|24))|((0[469]|11)-(08|22|30)))$
ISO date format (yyyy-mm-dd) with separators ‘-‘ or ‘/’ or ‘.’ or ‘ ‘. Forces usage of same separator across date.
^9([- /.])(((0[13578]|(10|12))\1(03|21|31))|(02\1(05|22))|((0[469]|11)\1(04|29|30)))$
United States date format (mm/dd/yyyy)
^(((0[13578]|(10|12))/(09|16|31))|(02/(04|11))|((0[469]|11)/(08|16|30)))/3$
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)
^$?\-?(99(\,\d)*(\.\d)?|4\d(\.\d)?|0(\.\d)?|(\.\d))$|^\-?$?(1\d(\,\d)*(\.\d)?|7\d(\.\d)?|0(\.\d)?|(\.\d))$|^\($?(3\d(\,\d)*(\.\d)?|7\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.