Skip to content

Regex Almanac

Moti Barski edited this page Apr 10, 2025 · 1 revision

Regex Almanac

Overview

A high-use Regex collection for the RegexUtil class.


Regex Between Two Strings

  • (?<=(one two)).*?(?=four)
    Example: one two three four → three

  • (?<=^).*?(?=$)
    Returns the whole string between start and finish.

  • (?<=one).*?(?=four)
    Example: one two three four → two three


Complete String If

  • ^.*(person|server).*$
    Example:
    • This person is cool → This person is cool
    • vanil stick → nothing
    • server one → server one

Contains None Of These

  • ^((?!.*(server|choco).*).)*$
    Example:
    • vanil stick → vanil stick
    • server one → nothing

Surname (Second Word)

  • \\s+[^\\s]+
    Example: mister meeseeks → meeseeks

Double

  • \\d+(\\.\\d+)?
    Example: 20.05

URL

  • [A-Za-z0-9.-]+\\.[A-Za-z]{2,6]

IPv4

  • ([0-9].){4}[0-9]*
    Example: 10.10.10.126

Phone Number Examples

Example 1

  • 0\\d-\\d{7}
    Example:
    • 02-1234567
    • Regex: ^0\\d-\\d{7}$

Example 2

  • [0]\\d{9}
    Phone starting with 0 followed by 9 digits.

Duplicate Word

  • \\b(\\w+)\\b(?=.*\\b\\1\\b)
    Example: hadoken hadoken shoryuken → hadoken
  • \\b([\\w\\s']+) \\1\\b
    Example: hadoken hadoken shoryuken → hadoken hadoken

Integer

  • [-+]?[0-9]{1,13}

Name and Surname

  • [A-Z][a-z]*\s[A-Z][a-z]*
    Example: Heihachi Mishima

Negative/Positive Double

  • [-+]?[0-9]*[.,][0-9]*
    Example: -30.77

Full Date

  • [0-9]{1,4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}
    Example: 2023/09/29 14:30:45

Timestamp

  • [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}11:52:31
  • [0-9]{1,2}:[0-9]{1,2}11:42

Email

  • [A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6]

  • ^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9.-]*element[a-zA-Z0-9.-]*)\.[a-zA-Z]{2,}$
    Emails with "element" in them (good for bans).

  • ^*@gmail.[a-zA-Z]{2,}$
    Emails from providers like Gmail (good for bans).


Any String

  • ^.+$

Metacharacters

  • + : Contains 1 or more of a character.
  • * : Contains 0 or more of a character.
  • ? : Contains 0 or 1 of a character.
  • (abc)+ : Matches "abc" or "abcabcabc."
  • [a-z] : Matches lowercase letters; [ab] matches "a" or "b."
  • {5} : Quantifier; e.g., ab{2}cabbc
  • | : OR; e.g., apple|banana matches "apple" or "banana."
  • . : Any character; e.g., a.c → "abc," "a1c," "acc."
  • ^ : String start.
  • $ : String end.

Fancy Numbers

  • (0x)?[0-9a-fA-F]+ : Hexadecimal.
  • \\b[01]+\\b : Binary.
  • (?<=0.).* : Removes leading zero.
  • M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}) : Roman numerals.
  • [0-9]{1,3}(,[0-9]{3})*\.[0-9]+ : Numbers with thousand separators.

First Word in a String

  • ^\\w+
    Example: one two three → one

Last Word in a String

  • \\w+$
    Example: one two three four → four

Number Stripper

  • [^\\d]+
    Example: h3llo → h llo

Clone this wiki locally