Regular Expressions 101

Save & Share

  • Regex Version: ver. 8
  • 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
No Match

r"
"
gum

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"(([\w\s]+)\(([1|2]\d{3})\))(.*)" test_str = ("Abbott A (1988) The System of Professions: An Essay on the Division of Expert Labor. Chicago,\n" "IL: University of Chicago Press.\n" "Albu OB and Flyverbom M (2016) Organizational transparency: conceptualizations, con-\n" "ditions, and consequences. Business & Society. Epub ahead of print 13 July. DOI:\n" "10.1177/0007650316659851.\n" "Ananny M (2016) Toward an ethics of algorithms: convening, observation, probability, and timeli-\n" "ness. Science, Technology & Human Values 41(1): 93–117. DOI: 10.1177/0162243915606523.\n" "Anderson CW (2013) Towards a sociology of computational and algorithmic journalism. New\n" "Media & Society 15(7): 1005–1021. DOI: 10.1177/1461444812465137.\n" "Arnoldi J (2015) Computer algorithms, market manipulation and the institutionalization of high\n" "frequency trading. Theory, Culture & Society 33: 29–52.\n" "Bacon M (2012) Pragmatism. Cambridge: Polity Press.\n" "Baecker R (1998) Sorting out sorting: a case study of software visualization for teaching computer\n" "science. In: Stasko J (ed.) Software Visualization Programming as a Multimedia Experience.\n" "Cambridge, MA: The MIT Press, pp. 369–381.\n" "Ball C (2009) What is transparency? Public Integrity 11(4): 293–308.\n" "Barany MJ and MacKenzie D (2014) Chalk: materials and concepts in mathematics research. In:\n" "Coopmans C, Vertesi J, Lynch M, et al. (eds) Representation in Scientific Practice Revisited.\n" "Cambridge, MA: The MIT Press, pp. 107–130.\n" "Bateson G (2000) Steps to an Ecology of Mind: Collected Essays in Anthropology, Psychiatry,\n" "Evolution, and Epistemology. Chicago, IL: University of Chicago Press.\n" "Birchall C (2011) Introduction to “secrecy and transparency”: the politics of opacity and openness.\n" "Theory, Culture & Society 28(7–8): 7–25.\n" "Birchall C (2014) Radical transparency? Cultural Studies ↔ Critical Methodologies 14(1): 77–88.\n" "Bratich J (2016) Occult(ing) transparency: an epilogue. International Journal of Communication\n" "Systems 10: 178–181.\n" "Brill J (2015) Scalable approaches to transparency and accountability in decisionmaking algo-\n" "rithms: remarks at the NYU conference on algorithms and accountability. Federal Trade\n" "Commission, 28 February. Available at: https://www.ftc.gov/system/files/documents/pub-\n" "lic_statements/629681/150228nyualgorithms.pdf\n" "Brown MH (1998) A taxonomy of algorithm animation displays. In: Stasko J (ed.) Software\n" "Visualization Programming as a Multimedia Experience. Cambridge, MA: The MIT Press,\n" "pp. 35–42.\n" "Browne S (2015) Dark Matters: On the Surveillance of Blackness. Durham, NC: Duke University\n" "Press.\n" "Burrell J (2016) How the machine “thinks”: understanding opacity in machine learning algorithms.\n" "Big Data & Society 3(1). DOI: 10.1177/2053951715622512.\n" "Carusi A and Hoel AS (2014) Toward a new ontology of scientific vision. In: Coopmans C, Vertesi\n" "J, Lynch M, et al. (eds) Representation in Scientific Practice Revisited. Cambridge, MA: The\n" "MIT Press, pp. 201–221.\n" "Christensen LT and Cheney G (2015) Peering into transparency: challenging ideals, proxies, and\n" "organizational practices. Communication Theory 25(1): 70–90.\n" "Coddington M (2012) Defending a paradigm by patrolling a boundary: two global newspapers’\n" "approach to WikiLeaks. Journalism & Mass Communication Quarterly 89(3): 377–396.\n" "Coleman G (2013) Coding Freedom: The Ethics and Aesthetics of Hacking. Princeton, NJ:\n" "Princeton University Press.\n" "Coopmans C, Vertesi J, Lynch M, et al. (2014) Introduction. In: Coopmans C, Vertesi J, Lynch M,\n" "et al. (eds) Representation in Scientific Practice Revisited. Cambridge, MA: The MIT Press,\n" "pp. 1–12.\n" "Crain M (2016) The limits of transparency: data brokers and commodification. New Media &\n" "Society. Epub ahead of print 7 July. DOI: 10.1177/1461444816657096.\n" "Crain W (2000) Theories and Development: Concepts and Applications. Upper Saddle River, NJ:\n" "Prentice Hall.\n" "Crary J (1990) Techniques of the Observer. Cambridge, MA: The MIT Press.\n" "Crawford K (2016) Can an algorithm be agonistic? Ten scenes from life in calculated publics.\n" "Science, Technology & Human Values 41(1): 77–92.\n" "da Vinci L (2015) Notebooks of Leonardo Da Vinci (trans. JP Richter). Istanbul: eKitap Projesi.\n" "Daston L (1992) Objectivity and the escape from perspective. Social Studies of Science 22(4):\n" "597–618.\n" "Daston L and Galison P (2007) Objectivity. Brooklyn, NY: Zone Books.\n" "Datta A, Tschantz MC and Datta A (2015) Automated experiments on ad privacy settings.\n" "Proceedings on Privacy Enhancing Technologies 1: 92–112.\n" "David M (2015) The correspondence theory of truth. Stanford Encyclopedia of Philosophy, 28\n" "May Available at: http://plato.stanford.edu/entries/truth-correspondence/\n" "De Rijcke S and Beaulieu A (2014) Networked neuroscience: brain scans and visual knowing at\n" "the intersection of atlases and databases. In: Coopmans C, Vertesi J, Lynch M, et al. (eds)\n" "Representation in Scientific Practice Revisited. Cambridge, MA: The MIT Press, pp. 131–\n" "152.\n" "Dewey J (1997) Experience and Education. New York: Free Press.\n" "Diakopoulos N (2016) Accountability in algorithmic decision making. Communications of the\n" "ACM 59(2): 56–62.\n" "DiSalvo C (2012) Adversarial Design. Cambridge, MA: The MIT Press.\n" "Dougherty C (2015) Google photos mistakenly labels black people “gorillas.” The New York\n" "Times, 1 July. Available at: http://bits.blogs.nytimes.com/2015/07/01/google-photos-mistak-\n" "enly-labels-black-people-gorillas/\n" "Driscoll K and Walker S (2014) Working within a black box: transparency in the collection\n" "and production of big Twitter data. International Journal of Communication Systems 8:\n" "1745–1764.\n" "Eslami M, Rickman A, Vaccaro K, et al. (2015) “I always assumed that I wasn’t really that close\n" "to [her]”: reasoning about invisible algorithms in the news feed. In: Proceedings of the 33rd\n" "annual ACM conference on human factors in computing systems, Seoul, Republic of Korea,\n" "18–23 April.\n" "Flyverbom M (2016) Transparency: mediation and the management of visibilities. International\n" "Journal of Communication Systems 10: 110–122.\n" "Fox J (2007) The uncertain relationship between transparency and accountability. Development in\n" "Practice 17(4–5): 663–671.\n" "Fung A, Graham M and Weil D (2008) Full Disclosure: The Perils and Promise of Transparency.\n" "Cambridge: Cambridge University Press.\n" "Gillespie T (2014) The relevance of algorithms. In: Gillespie T, Boczkowski P and Foot KA (eds)\n" "Media Technologies: Essays on Communication, Materiality, and Society. Cambridge, MA:\n" "The MIT Press, pp. 167–194.\n" "Glasser TL (1989) Three views on accountability. In: Dennis EE, Gillmor DM and Glasser TL\n" "(eds) Media Freedom and Accountability. New York: Praeger.\n" "Goodwin C (1994) Professional vision. American Anthropologist 96(3): 606–633.\n" "Greenwald G (2014) No Place to Hide. New York: Metropolitan Books.\n" "Hallinan B and Striphas T (2014) Recommended for you: the Netflix Prize and the production of\n" "algorithmic culture. New Media & Society 18: 117–137.\n" "Hansen HK and Flyverbom M (2015) The politics of transparency and the calibration of knowl-\n" "edge in the digital age. Organization 22(6): 872–889.\n" "Heald D (2006) Varieties of transparency. Proceedings of the British Academy 135: 25–43.\n" "Heemsbergen L (2016) From radical transparency to radical disclosure: reconfiguring (in)vol-\n" "untary transparency through the management of visibilities. International Journal of\n" "Communication Systems 10: 138–151.\n" "Hilgartner S (2000) Science on Stage: Expert Advice as Public Drama. Stanford, CA: Stanford\n" "University Press.\n" "Hood C (2006) Transparency in historical perspective. In: Hood C and Heald D (eds) Transparency:\n" "The Key to Better Governance? Oxford: Oxford University Press, pp. 1–23.\n" "James W (1997) What pragmatism means. In: Menand L (ed.) Pragmatism: A Reader. New York:\n" "Random House, pp. 93–111.\n" "Kelty C (2008) Two Bits: The Cultural Significance of Free Software. Durham, NC: Duke\n" "University Press.\n" "Kushner S (2013) The freelance translation machine: algorithmic culture and the invisible indus-\n" "try. New Media & Society 15: 1241–1258.\n" "Leigh D and Harding L (2011) WikiLeaks: Inside Julian Assange’s War on Secrecy. New York:\n" "Public Affairs.\n" "Levy KEC (2015) The contexts of control: information, power, and truck-driving work. Information\n" "Society 31(2): 160–174.\n" "Levy KEC and Johns DM (2016) When open data is a Trojan Horse: the weaponization of trans-\n" "parency in science and governance. Big Data & Society 3(1). http://bds.sagepub.com/con-\n" "tent/3/1/2053951715621568\n" "Lynch M and Woolgar S (1988) Introduction: sociological orientations to representational practice\n" "in science. Human Studies 11(2): 99–116.\n" "Mackenzie A (2015) The production of prediction: what does machine learning want? European\n" "Journal of Cultural Studies 18(4–5): 429–445.\n" "Mackenzie D (2008) An Engine, Not a Camera: How Financial Models Shape Markets. Cambridge,\n" "MA: The MIT Press.\n" "Michaels D (2008) Doubt Is Their Product: How Industry’s Assault on Science Threatens Your\n" "Health. Oxford: Oxford University Press.\n" "Minsky M (2007) The Emotion Machine: Commonsense Thinking, Artificial Intelligence, and the\n" "Future of the Human Mind. New York: Simon & Schuster.\n" "Misak C (ed.) (2007) New Pragmatists. Oxford: Oxford University Press.\n" "Napoli PM (2014) Automated media: an institutional theory perspective on algorithmic media\n" "production and consumption. Communication Theory 24(3): 340–360.\n" "Neyland D and Möllers N (2016) Algorithmic IF ... THEN rules and the conditions and conse-\n" "quences of power. Information, Communication & Society, 20 (1): 45-62.\n" "Papert S (1980) Mindstorms: Children, Computers, and Powerful Ideas. New York: Basic\n" "Books.\n" "Pasquale F (2015) The Black Box Society: The Secret Algorithms That Control Money and\n" "Information. Cambridge, MA: Harvard University Press.\n" "Phillips JWP (2011) Secrecy and transparency: an interview with Samuel Weber. Theory, Culture\n" "& Society 28(7–8): 158–172.\n" "Resnick M, Berg R and Eisenberg M (2000) Beyond black boxes: bringing transparency and aes-\n" "thetics back to scientific investigation. Journal of the Learning Sciences 9(1): 7–30.\n" "Schnackenberg AK and Tomlinson EC (2014) Organizational transparency: a new perspective\n" "on managing trust in organization-stakeholder relationships. Journal of Management 42:\n" "1784–1810.\n" "Schrock AR (2016) Civic hacking as data activism and advocacy: a history from publicity to open\n" "government data. New Media & Society 18: 581–599.\n" "Schudson M (2015) The Rise of the Right to Know. Cambridge, MA: Belknap Publishing.\n" "Squires CR (2002) Rethinking the black public sphere: an alternative vocabulary for multiple\n" "public spheres. Communication Theory 12(4): 446–468.\n" "Stiny G (1980) Kindergarten grammars: designing with Froebel’s building gifts. Environment and\n" "Planning B 7(4): 409–462.\n" "Stohl C, Stohl M and Leonardi PM (2016) Managing opacity: information visibility and the para-\n" "dox of transparency in the digital age. International Journal of Communication Systems 10:\n" "123–137.\n" "Striphas T (2015) Algorithmic culture. European Journal of Cultural Studies 18(4–5): 395–412.\n" "Tkacz N (2015) Wikipedia and the Politics of Openness. Chicago, IL: University of Chicago Press.\n" "Turner F (2006) From Counterculture to Cyberculture: Stewart Brand, the Whole Earth Network,\n" "and the Rise of Digital Utopianism. Chicago, IL: University of Chicago Press.\n" "Turner F (2013) The Democratic Surround. Chicago, IL: University of Chicago Press.\n" "Vertesi J (2014) Drawing as: distinctions and disambiguation in digital images of Mars. In:\n" "Coopmans C, Vertesi J, Lynch M, et al. (eds) Representation in Scientific Practice Revisited.\n" "Cambridge, MA: The MIT Press, pp. 15–36.\n" "Wade L (2010) HP software doesn’t see black people. Sociological Images, 5 January. Available\n" "at: https://thesocietypages.org/socimages/2010/01/05/hp-software-doesnt-see-black-people/\n" "Ward SC (1996) Reconfiguring Truth. New York: Rowman & Littlefield.\n" "Zara C (2015) FTC chief technologist Ashkan Soltani on algorithmic transparency and the\n" "fight against biased bots. International Business Times, 9 April. Available at: http://www.\n" "ibtimes.com/ftc-chief-technologist-ashkan-soltani-algorithmic-transparency-fight-against-\n" "biased-1876177") matches = re.finditer(regex, test_str, re.UNICODE | 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