Java replace regexp online

Want to support RegExr? Consider disabling your ad-blocker for this domain. We’ll show a non-intrusive, dev-oriented ad in this area.

Expression

Create new tests with the ‘Add Test’ button. Click a test to edit the name, type, & text.

Tools

RegEx Engine

Expression Flags

Sign In

Sign in to persist favorites & patterns. Click the help icon above for info.

Any unsaved changes will be lost. Saved patterns & favorites will be migrated to your account.

Sign Out

You are currently signed in as via .

Character classes
. any character except newline
\w \d \s word, digit, whitespace
\W \D \S not word, digit, whitespace
[abc] any of a, b, or c
[^abc] not a, b, or c
[a-g] character between a & g
Anchors
^abc$ start / end of the string
\b \B word, not-word boundary
Escaped characters
\. \* \\ escaped special characters
\t \n \r tab, linefeed, carriage return
Groups & Lookaround
(abc) capture group
\1 backreference to group #1
(?:abc) non-capturing group
(?=abc) positive lookahead
(?!abc) negative lookahead
Quantifiers & Alternation
a* a+ a? 0 or more, 1 or more, 0 or 1
a a exactly five, two or more
a between one & three
a+? a? match as few as possible
ab|cd match ab or cd

Источник

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.

^(?:(?:255|229|[01]?92?)\.)(?:253|221|[01]?97?)$
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)

^6-(((0[13578]|(10|12))-(02|14|31))|(02-(07|21))|((0[469]|11)-(03|22|30)))$

ISO date format (yyyy-mm-dd) with separators ‘-‘ or ‘/’ or ‘.’ or ‘ ‘. Forces usage of same separator across date.

^1([- /.])(((0[13578]|(10|12))\1(03|19|31))|(02\1(04|23))|((0[469]|11)\1(03|15|30)))$

United States date format (mm/dd/yyyy)

^(((0[13578]|(10|12))/(04|23|31))|(02/(02|25))|((0[469]|11)/(09|25|30)))/2$

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)

^$?\-?(21(\,\d)*(\.\d)?|5\d(\.\d)?|0(\.\d)?|(\.\d))$|^\-?$?(8\d(\,\d)*(\.\d)?|7\d(\.\d)?|0(\.\d)?|(\.\d))$|^\($?(9\d(\,\d)*(\.\d)?|6\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 replace regexp online

You seem to be using an outdated version of your browser which is no longer supported by regexpr.com. It is highly recommended that you upgrade your browser. Sorry for the inconvenience.

Оцените статью