Skip to content

blinfo/RegularExpressions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This file is auto generated. Any changes made to it will disappear with next build.

Wiki page

Exercises

# Regular Expressions

Single digit

Expression: \\d

Alternative: [0-9]

Matches exactly one digit.

Matches:

  • true "1"
  • true "7"

Non Matches:

  • false "a"
  • false "P"
  • false "@"
  • false "11"

With alternative writing:

Matches:

  • true "1"
  • true "7"

Non Matches:

  • false "a"
  • false "P"
  • false "@"
  • false "11"

^


Single non digit

Expression: \\D

Alternative: [^0-9]

Matches exactly one character which can not be a digit. The circumflex as the first character in the brackets means "NOT"

Matches:

  • true "a"
  • true "P"
  • true "@"
  • true "."

Non Matches:

  • false "1"
  • false "7"
  • false "pt"

With alternative writing:

Matches:

  • true "a"
  • true "P"
  • true "@"
  • true "."

Non Matches:

  • false "1"
  • false "7"
  • false "pt"

^


Single dot

Expression: \\.

Matches exactly one dot (.). The dot is a meta character and must be escaped.

Matches:

  • true "."

Non Matches:

  • false "1"
  • false ","
  • false ".."

^


Single hyphen

Expression: \\-

Alternative: [-]

Matches exactly one hyphen or minus (-). The minus sign is a meta character and must be escaped unless it occurs first or last in a square bracket group: "[-+a-z]" which matches plus, minus or lower case letters a through z, or "[0-5-]" which matches a minus or number zero through five.

Matches:

  • true "-"

Non Matches:

  • false "?"
  • false "_"
  • false "--"

With alternative writing:

Matches:

  • true "-"

Non Matches:

  • false "?"
  • false "_"
  • false "--"

^


Zero or more digits

Expression: \\d*

Alternative: [0-9]{0,}

Matches zero or more digits.

Matches:

  • true ""
  • true "7"
  • true "1798451451"

Non Matches:

  • false "a"
  • false "P"
  • false "@"

With alternative writing:

Matches:

  • true ""
  • true "7"
  • true "1798451451"

Non Matches:

  • false "a"
  • false "P"
  • false "@"

^


Zero or one digit

Expression: \\d?

Alternative: \\d{0,1}

Matches zero or one digit.

Matches:

  • true ""
  • true "7"
  • true "4"

Non Matches:

  • false "a"
  • false "11"
  • false "957"

With alternative writing:

Matches:

  • true ""
  • true "7"
  • true "4"

Non Matches:

  • false "a"
  • false "11"
  • false "957"

^


One or more digits

Expression: \\d+

Alternative: [0-9]{1,}

Matches one or more digits.

Matches:

  • true "0"
  • true "14"
  • true "18155111"

Non Matches:

  • false "a"
  • false "P"
  • false "@"

With alternative writing:

Matches:

  • true "0"
  • true "14"
  • true "18155111"

Non Matches:

  • false "a"
  • false "P"
  • false "@"

^


Three digits

Expression: \\d{3}

Alternative: [0-9]{3}

Matches exactly three digits.

Matches:

  • true "123"
  • true "789"
  • true "048"

Non Matches:

  • false "1"
  • false "22"
  • false "1234"
  • false "a"
  • false ""

With alternative writing:

Matches:

  • true "123"
  • true "789"
  • true "048"

Non Matches:

  • false "1"
  • false "22"
  • false "1234"
  • false "a"
  • false ""

^


Three or more digits

Expression: \\d{3,}

Alternative: [0-9]{3,}

Matches three or more digits.

Matches:

  • true "123"
  • true "4567"
  • true "1215854521"

Non Matches:

  • false "a"
  • false "1"
  • false "83"
  • false "a42"

With alternative writing:

Matches:

  • true "123"
  • true "4567"
  • true "1215854521"

Non Matches:

  • false "a"
  • false "1"
  • false "83"
  • false "a42"

^


Three to five digits

Expression: \\d{3,5}

Alternative: [0-9]{3,5}

Matches three through five digits.

Matches:

  • true "123"
  • true "4567"
  • true "89012"

Non Matches:

  • false "12"
  • false "345678"
  • false "text"

With alternative writing:

Matches:

  • true "123"
  • true "4567"
  • true "89012"

Non Matches:

  • false "12"
  • false "345678"
  • false "text"

^


Hexadecimal

Expression: [\\da-fA-F]

Alternative: [0-9a-fA-F]

Matches one hexadecimal.

Matches:

  • true "1"
  • true "8"
  • true "A"
  • true "f"

Non Matches:

  • false ""
  • false "11"
  • false "G"
  • false "h"

With alternative writing:

Matches:

  • true "1"
  • true "8"
  • true "A"
  • true "f"

Non Matches:

  • false ""
  • false "11"
  • false "G"
  • false "h"

^


Negative or positive integer

Expression: [+\\-]?\\d+

Alternative: [-+]?[0-9]{1,}

Matches negative or positive integer of one or more digits. To use a minus (hyphen) in a group you can put it as the first character in the group, the first character after a negating circumflex, the last character before the closing bracket or escape it.

Matches:

  • true "0"
  • true "-19"
  • true "4"
  • true "-534"
  • true "+24"
  • true "-0"

Non Matches:

  • false "-"
  • false "1+1"
  • false "-11.2"
  • false ""

With alternative writing:

Matches:

  • true "0"
  • true "-19"
  • true "4"
  • true "-534"
  • true "+24"
  • true "-0"

Non Matches:

  • false "-"
  • false "1+1"
  • false "-11.2"
  • false ""

^


Zero or negative or positive integer

Expression: 0|[-+]?[1-9]\\d{0,}

Matches number zero or a negative or positive non zero of one or more digits.

Matches:

  • true "0"
  • true "-19"
  • true "4"
  • true "-534"
  • true "+24"

Non Matches:

  • false "-"
  • false "-0"
  • false "1+1"
  • false "-11.2"
  • false ""

^


Zero or negative or positive float

Expression: 0|[-+]?\\d+\\.\\d+

Matches number zero or a negative or positive float (non zero) with required zero before floating point.

Matches:

  • true "0.1"
  • true "-19.0"
  • true "4.0154"
  • true "-0.534"
  • true "+0.24"

Non Matches:

  • false "-"
  • false "4"
  • false "-0"
  • false "1+1"
  • false "-11,2"
  • false ""
  • false "1.2.43"

^


Negative or positive float or integer

Expression: [-+]?\\d*\\.?\\d+

Matches negative or positive float or integer with optional zero before floating point.

Matches:

  • true "0.1"
  • true ".5"
  • true "-19.0"
  • true "3.13159"
  • true "-0.534"
  • true "+0.24"
  • true "-.4334546"
  • true "11"
  • true "0"

Non Matches:

  • false "-"
  • false "a4"
  • false "1+1"
  • false "-11,2"
  • false "1+"
  • false "1.2.43"

^


One English letter et al

Expression: \\w

Alternative: [a-zA-Z_0-9]

Matches one English letter, one digit or underscore. This character class is highly unreliable so the alternative writing is to be preferred.

Matches:

  • true "a"
  • true "Z"
  • true "y"
  • true "_"
  • true "8"

Non Matches:

  • false "å"
  • false "$"
  • false "tr"
  • false "ä"

With alternative writing:

Matches:

  • true "a"
  • true "Z"
  • true "y"
  • true "_"
  • true "8"

Non Matches:

  • false "å"
  • false "$"
  • false "tr"
  • false "ä"

^


One Swedish letter

Expression: [a-zA-ZåäöÅÄÖ]

Matches one Swedish letter.

Matches:

  • true "Ä"
  • true "z"
  • true "W"
  • true "ö"

Non Matches:

  • false "é"
  • false "à"
  • false "ł"
  • false "ß"

^


Some specific letters

Expression: [0-9a-fQXZ£€$]

Matches digit, lower case letters a through f, upper case letters Q, X or Z and money symbols £, € and $.

Matches:

  • true "a"
  • true "9"
  • true "Z"
  • true "$"
  • true "€"

Non Matches:

  • false "A"
  • false "x"
  • false "y"
  • false "¥"

^


Consonants

Expression: [a-zA-Z&&[^aeiouyAEIOUY]]

Alternative: [b-df-hj-np-tv-xzB-DF-HJ-NP-TV-XZ]

Matches all consonants but no wovels

Matches:

  • true "b"
  • true "Q"
  • true "Z"
  • true "v"
  • true "L"

Non Matches:

  • false "A"
  • false "e"
  • false "y"
  • false "O"

With alternative writing:

Matches:

  • true "b"
  • true "Q"
  • true "Z"
  • true "v"
  • true "L"

Non Matches:

  • false "A"
  • false "e"
  • false "y"
  • false "O"

^


Dot - Any character but new line

Expression: .

Matches any character that is not a new line. The non matching characters in the example below are \r\n, \r, \n

Matches:

  • true "b"
  • true "Q"
  • true "?"
  • true "!"
  • true "@"
  • true "¡"
  • true "1"
  • true "8"

Non Matches:

  • false " "
  • false " "
  • false " "

^


Single white space

Expression: \\s

Matches whitespace characters, like new line, carriage return, tab and space. The matching characters in the example below are \r\n, \r, \n, space and \f

Matches:

  • true " "
  • true " "
  • true " "
  • true " "
  • true " "

Non Matches:

  • false ""
  • false "x"

^


Single non white space

Expression: \\S

Alternative: [^\\s]

Matches non whitespace characters, like letter, digit or other symbol. The non matching characters in the example below are \r, \n, \t, space and \f

Matches:

  • true "a"
  • true "x"
  • true "Ö"
  • true "+"
  • true "0"
  • true "&"
  • true "¥"

Non Matches:

  • false " "
  • false " "
  • false " "
  • false " "
  • false " "

With alternative writing:

Matches:

  • true "a"
  • true "x"
  • true "Ö"
  • true "+"
  • true "0"
  • true "&"
  • true "¥"

Non Matches:

  • false " "
  • false " "
  • false " "
  • false " "
  • false " "

^


New line

Expression: \\R

Alternative: \\r?\\n

Matches new line both in windows and unix-like os (mac). Windows uses CRLF (\r\n) while other uses only LF (\n). The strings that matches below are \n and \r\n, the non-matching strings are \n\r and \t.

Matches:

  • true " "
  • true " "

Non Matches:

  • false "

"

  • false " "

With alternative writing:

Matches:

  • true " "
  • true " "

Non Matches:

  • false "

"

  • false " "

^


Single white space or digit

Expression: [\\s\\d]

Matches whitespace characters, like new line, carriage return, tab and space and a digit.

Matches:

  • true " "
  • true " "
  • true " "
  • true " "
  • true "1"
  • true "9"

Non Matches:

  • false ""
  • false "x"
  • false "11"
  • false "A"

^


Role playing die

Expression: \\d+[dDtT]\\d+([-+]?\\d*)?

Matches the typical role playing die pattern: 3D6+2, 1D3-1, 4D8 and the Swedish model 4T6. Does not care about the number of sides of the typical dice.

Matches:

  • true "2D6"
  • true "4d8"
  • true "1T20"
  • true "5D4+5"
  • true "1D100"
  • true "9D2-9"

Non Matches:

  • false "D10"
  • false "2C4"
  • false "11"

^


Role playing die with grouping

Expression: (\\d+)[dDtT](\\d+)([-+]?)(\\d*)

Almost the same as the previous, but with grouping for parsing like in the ImprovedRolePlayingDieParser or the javascript version, role-playing-dice-parser.js.

Matches:

  • true "2D6"
  • true "4d8"
  • true "1T20"
  • true "5D4+5"
  • true "1D100"
  • true "9D2-9"

Non Matches:

  • false "D10"
  • false "2C4"
  • false "11"

^


Date - a fairly good match

Expression: (19|20)\\d{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])

A fairly good matching of dates. It does not handel the number of days for each month and consequently not leap years. Thus an invalid date like "1997-02-31" will still match. To add another century, add a it to the start of the expression: "(18|19|20)\d{2}", which would place the failed date "1874-11-15" within the range for the expression and make it match.

Matches:

  • true "1920-12-31"
  • true "2037-11-19"
  • true "2000-01-01"
  • true "1968/08/19"
  • true "1997 02 31"

Non Matches:

  • false "1874-11-15"
  • false "1989-13-01"
  • false "2084-11-00"

^


ISO-Date

Expression: (19|20)\\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])

The same as above but only for ISO-date (ISO 8601 - yyyy-mm-dd).

Matches:

  • true "1920-12-31"
  • true "2037-11-19"
  • true "2000-01-01"

Non Matches:

  • false "1874-11-15"
  • false "1989-13-01"
  • false "1968/08/19"
  • false "2084-11-00"
  • false "1997 02 31"

^


Swedish social security number - Initial attempt

Expression: (\\d{2})?\\d{6}-?\\d{4}

Simple matching of the Swedish social security number. Six or eight digits, an optional hyphen and four digits. This expressions only checks for the right number or digits, not whether they are correct.

Matches:

  • true "254789-1234"
  • true "987741008475"
  • true "12345678-0987"
  • true "8745632104"

Non Matches:

  • false "1234567-9781"
  • false "1920-12-11-5789"
  • false "647189-987"
  • false "87456321"

^


Swedish social security number - Improved

Expression: (?:19|20)?\\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1-2]\\d|3[0-1])-?\\d{4}

Alternative: ((?:19|20)?(\\d{2}))(0[1-9]|1[0-2])(0[1-9]|[1-2]\\d|3[0-1])[- ]?(\\d{4})

A somewhat better matching of the Swedish social security number. Six or eight digits, an optional hyphen and four digits. Note that the first four non-matching numbers matched the initial attempt. To make a validation of the last digit (control digit) you need to use more than a regexp. The alternative expression contains the grouping used in the SwedishSocialSecurityNumberValidator and the javascript swedish-social-security-number-validator.js.

Matches:

  • true "19721011-6534"
  • true "7811309574"
  • true "120226-0987"
  • true "19991231-9999"

Non Matches:

  • false "254789-1234"
  • false "987741008475"
  • false "12345678-0987"
  • false "8745632104"
  • false "1234567-9781"
  • false "1920-12-11-5789"
  • false "647189-987"
  • false "87456321"
  • false "20040832-1541"

With alternative writing:

Matches:

  • true "19721011-6534"
  • true "7811309574"
  • true "120226-0987"
  • true "19991231-9999"

Non Matches:

  • false "254789-1234"
  • false "987741008475"
  • false "12345678-0987"
  • false "8745632104"
  • false "1234567-9781"
  • false "1920-12-11-5789"
  • false "647189-987"
  • false "87456321"
  • false "20040832-1541"

^


Email address - A pretty rudimentary example

Expression: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}

Matches email address pattern.

Matches:

Non Matches:

^


Swedish car number

Expression: [A-HJ-PR-UW-Z]{3} ?(0\\d[A-HJ-PR-UW-Z1-9]|[1-9]\\d[A-HJ-PR-UW-Z0-9])

Alternative: [A-Z&&[^IQV]]{3} ?(0\\d[A-Z1-9&&[^IQV]]|[1-9]\\d[A-Z0-9&&[^IQV]])

Matches the Swedish licence plate number. Three upper case letters A-H, J-P, R-U or W-Z followed by a single space and then 001 - 999 or 00 - 99 plus upper case letter A-H, J-P, R-U or W-Z. Some letter combinations are removed, like KKK, PKK and a few others, but this expression does not handle these numbers.

Matches:

  • true "ABC123"
  • true "PKJ 001"
  • true "DEF 456"
  • true "HJK 94A"
  • true "AAA 00W"

Non Matches:

  • false "abc123"
  • false "CDE 000"
  • false "IIV 453"
  • false "DEF 4567"
  • false "QHI 987"
  • false "BCD 12I"

With alternative writing:

Matches:

  • true "ABC123"
  • true "PKJ 001"
  • true "DEF 456"
  • true "HJK 94A"
  • true "AAA 00W"

Non Matches:

  • false "abc123"
  • false "CDE 000"
  • false "IIV 453"
  • false "DEF 4567"
  • false "QHI 987"
  • false "BCD 12I"

^

About

Samples and exercises for Regular Expressions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published