Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gm

Test String

Code Generator

Generated Code

$re = '/^\d*\n(.*)\n([^\d].*|)/m'; $str = '1 Mecca (مكة) Trading hub and sanctuary in pre-Islamic Arabia; holy city to Muslims; current capital of Makkah Province, Saudi Arabia 2 Medina (المدينة) Political seat of Muhammad, and first capital of the Rashidun Caliphate; current capital of Al Madinah Province, Saudi Arabia 3 Damascus (دمشق) Capital of the Umayyad dynasty; current capital of Syria 4 Baghdad (بغداد) Second capital of the Abbasid dynasty, and actual seat of Harun al-Rashid; current capital of Iraq 5 Najran (نجران) Christian center in 5th-7th century Arabia; current capital of Najran Province, Saudi Arabia 6 Kufah (الكوفة) Second capital of the Rashidun Caliphate under Ali\'s rule; first capital of the Abbasid dynasty; city in modern-day Iraq 7 Basra (البصرة) City in Iraq 8 Khurasan (خراسان) Region corresponding to modern Afghanistan and northeast Iran 9 Anjar (عنجر) City in Lebanon 10 Fustat (الفسطاط) Old city of Cairo 11 Aden (عدن) City in Yemen 12 Yamama (اليمامة) Modern Najd region of Saudi Arabia 13 Muscat (مسقط) Capital of Oman 14 Mansura (المنصورة) City in Egypt 15 Bukhara (بخارى) Modern day Buxoro, capital of Buxoro Province, Uzbekistan 16 Fez (فاس) Shared with Morocco, not buildable if they are in the game 17 Shiraz (شيراز) Capital of Fārs Province, Iran 18 Merw (ميرف) Modern day Mary; capital of Mary Province, Turkmenistan 19 Balkh (بلخ) City in Afghanistan 20 Mosul (الموصل) City in Iraq 21 Aydab (؟؟؟؟؟) 22 Bayt Ras (؟؟؟؟؟؟) 23 Suhar (صحار) City in Oman 24 Taif (طائف) City in Saudi Arabia 25 Hama (حماة) Capital of Hamāh Governorate, Syria 26 Tabuk (تبوك) Capital of Tabūk Province, Saudi Arabia 27 Sana\'a (صنعاء) Capital of Yemen 28 Shihr (الشحر) City in Yemen 29 Tripoli (طرابلس) Capital of Libya 30 Tunis (تونس) Capital of Tunisia 31 Kairouan (القيروان) City in Tunisia 32 Algiers (الجزائر) Capital of Algeria 33 Oran (وهران) City in Algeria 34 Tangier (طنجة) Shared with Morocco, not buildable if they are in the game 35 Casablanca (الدار البيضاء) Shared with Morocco, not buildable if they are in the game 36 Marrakech (مراكش) Shared with Morocco, not buildable if they are in the game'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php