Regular Expressions 101

Save & Share

  • Regex Version: ver. 44
  • 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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"(?=\{Infobox)(\{([^{}]|(?1))*\})" test_str = ("\n" "<?xml version=\"1.0\"?><api><query><pages><page pageid=\"5843419\" ns=\"0\" title=\"France\"><revisions><rev contentformat=\"text/x-wiki\" contentmodel=\"wikitext\" xml:space=\"preserve\">{{about|the country}}\n" "{{Use dmy dates|date=November 2013}}\n" "{{EngvarB|date=November 2013}}\n" "{{see also|Portal:France{{!}}France portal|Outline of France}}\n" "{{pp-semi|small=yes}}{{pp-move-indef}}\n" "{{Infobox country\n" "|conventional_long_name = French Republic\n" "|native_name = {{native name|fr|République française|icon=no}}\n" "|common_name = France\n" "|image_flag = Flag of France.svg\n" "|demonym = French\n" "|government_type = [[Unitary state|Unitary]] [[Semi-presidential system|semi-presidential]] [[republic|constitutional republic]]\n" "|leader_title1 = [[President of France|President]]\n" "|leader_name1 = [[François Hollande]]\n" "|calling_code = [[Telephone numbers in France|+33]]{{refn|group=note|name=eleven|The overseas regions and collectivities form part of the [[French telephone numbering plan]], but have their own country calling codes: [[Guadeloupe]] +590; [[Martinique]] +596; [[French Guiana]] +594, [[Réunion]] and [[Mayotte]] +262; [[Saint Pierre and Miquelon]] +508. The overseas territories are not part of the French telephone numbering plan; their country calling codes are: [[New Caledonia]] +687, [[French Polynesia]] +689; [[Wallis and Futuna]] +681.}}\n" "|ISO_3166-1_alpha2 =\n" "|ISO_3166-1_alpha3 = FRA\n" "|ISO_3166-1_numeric =\n" "|sport_code = FRA\n" "|vehicle_code = F\n" "|cctld = [[.fr]]{{refn|group=note|name=ten|In addition to [[.fr]], several other Internet TLDs are used in French overseas ''départements'' and territories: [[.re]], [[.mq]], [[.gp]], [[.tf]], [[.nc]], [[.pf]], [[.wf]], [[.pm]], [[.gf]] and [[.yt]]. France also uses [[.eu]], shared with other members of the European Union. The [[.cat]] domain is used in [[Catalan Countries|Catalan-speaking territories]].}}\n" "}}\n\n" "'''France''' ({{IPAc-en|UK|ˈ|f|r|ɑː|n|s}}; {{IPAc-en|US|audio=en-us-France.ogg|ˈ|f|r|æ|n|s}}; {{IPA-fr|fʁɑ̃s|lang|France.ogg}}), officially the '''French Republic''' ({{lang-fr|link=no|République française}} {{IPA-fr|ʁepyblik fʁɑ̃sɛz|}}), is a sovereign country in [[Western Europe]] that includes [[Overseas departments and territories of France|overseas regions and territories]].{{refn|group=note|name=twelve|[[French Guiana]] is located in [[South America]]; [[Guadeloupe]] and [[Martinique]] are in the [[Caribbean]]; and [[Réunion]] and [[Mayotte]] are in the [[Indian Ocean]], off the coast of [[Africa]]. All five [[Administrative divisions of France#Overseas|are considered integral parts of the republic]].}} [[Metropolitan France]] extends from the [[Mediterranean Sea]] to the [[English Channel]] and the [[North Sea]], and from the [[Rhine]] to the [[Atlantic Ocean]]. It is one of only three countries (besides [[Morocco]] and [[Spain]]) to have both Atlantic and Mediterranean coastlines. Due to its shape, it is often referred to in French as ''{{lang|fr|l’Hexagone}}'' (&quot;The [[Hexagon]]&quot;).\n\n" "France is the largest country in [[Western Europe]] and the [[European Union]], and the third-largest in Europe as a whole. With a total population of around 66 million, it is the [[List of European countries by population|third most-populous]] European country. France is a [[Unitary state|unitary]] [[Semi-presidential system|semi-presidential]] [[republic]] with its [[Capital city|capital]] in [[Paris]], the nation's largest city and the main cultural and commercial centre. The current [[Constitution of France]], adopted by referendum on 4 October 1958, establishes the country as secular and democratic, with its sovereignty derived from the people. The nation's ideals are expressed in the foundational ''[[Declaration of the Rights of Man and of the Citizen]]'', one of the world's earliest documents on human rights, which was formulated during the seminal [[French Revolution]] of the late 18th century.\n\n" "France has been a major power in Europe since the [[Late Middle Ages]], reaching the height of its influence during the 19th and early 20th centuries, when it possessed the [[French colonial empire|second-largest colonial empire]] of the time, and one of the largest in history. This legacy is reflected in the prevalence of [[French language]], [[Culture of France|culture]], and [[Napoleonic Code|jurisprudence]] worldwide. France has produced many influential artists, thinkers, and scientists, and remains a prominent global centre of culture. It hosts the world's [[List of World Heritage Sites in France|fourth-largest]] number of cultural [[UNESCO World Heritage Sites]], drawing around 83 million foreign tourists annually – the most of any country in the world.&lt;ref name=&quot;tourism.stat&quot;&gt;{{cite web |publisher= United Nations World Tourism Organization |url=http://dtxtq4w60xqpw.cloudfront.net/sites/all/files/pdf/unwto_highlights13_en_lr.pdf |title=UNWTO Highlights |accessdate=11 September 2013|format=PDF}}{{dead link|date=January 2014}}&lt;/ref&gt;\n\n" "France remains a [[major power]] with signif") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html