Regular Expressions 101

Save & Share

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

/
/
ig

Test String

Code Generator

Generated Code

import Foundation let pattern = #"^(“[^“”]+”)|\s(“[^“”]+”)|^("[^"]+")|\s("[^"]+")|^('.*')\s|\s('.*')\s|^('.*')$"# let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive) let testString = #""" 'Editor's note: Yu Xiangzhen is a retired editor and was a middle school student when the Cultural Revolution began 50 years ago in May 1966. What follows is her story, as told to CNN's Shen Lu, translated into English and edited for length and clarity. and won'afgfdt BEIJING (CNN) -- I have lived a life haunted by guilt. In 1966, I was one of Chairman Mao Zedong's Red Guards. Myself and millions of other middle and high school students started denouncing our teachers, friends, families and raiding homes and destroying other people's possessions. will give users the option of running or declining to run Flash on the site. “If the user accepts, Chr“ome” will “ advertise the presence of Flash Player and refresh the page,” Google said. On subsequent visits to the domain, the user's initial choice is lik Textbooks explain the Cultural Revolution -- in which hundreds of thousands of people were killed and millions more abused and traumatized -- as a political movement started and led by Mao "by mistake," but in reality it was a massive catastrophe for which we all bear responsibility. 'Closely follow Chairman Mao' On May 16, 1966, I was practicing calligraphy with my 37 classmates when a high-pitched voice came from the school's loudspeaker, announcing the central government's decision to start what it called a "Cultural Revolution." It was my first year of junior high, I was just 13. "Fellow students, we must closely follow Chairman Mao," the speaker bellowed. "Get out of the classroom! Devote yourselves to the Cultural Revolution!" Two boys rushed out of door, heading to the playground yelling something. <a href="http://www.bbc.com">hi</a> I left more slowly, holding hands with my best friend Haiyun as we followed everyone else outside. It would be my last normal day of school. Sent to the cowshed As Red Guards, we subjected anyone perceived as "bourgeois" or "revisionist" to brutal mental and physical attacks. I regret most what we did to our homeroom teacher Zhang Jilan. I was one of the most active students -- if not the most revolutionary -- when the class held a struggle session against Ms. Zhang. I pulled accusations out of nowhere, saying she was a heartless and cold woman, which was entirely false. Others accused her of being a Christian because the character "Ji" in her name could refer to Christianity. Our groundless criticisms were then written into "big character" posters -- a popular way of criticizing "class enemies" and spreading propaganda -- 60 of them in total, which covered the exterior walls of our classroom building. Not long after, she was sent to the cowshed -- a makeshift prison for intellectuals and other "bourgeois elements" -- and suffered all kinds of humiliation and abuse. It wasn't until 1990 that I saw her again. During a class trip to the Great Wall, we made a formal apology to Ms. Zhang -- then in her 80s -- for what we had subjected her to. We asked what had happened to her in the cowshed. "It wasn't too bad," she said. "I was made to crawl like a dog on the ground." Hearing this, I burst into tears. I was not yet 14, and I had made her life a misery. She died two years after our apology. Discomfort and guilt At the height of the movement in 1968, people were publicly beaten to death every day during struggle sessions; others who had been persecuted threw themselves off tall buildings. Nobody was safe and the fear of being reported by others -- in many cases our closest friends and family members -- haunted us. At first, I was determined to be a good little revolutionary guard. But something bothered me. When I saw a student pour a bucket of rotten paste over our school principal in 1966, I sensed something wasn't right. I headed back to my dorm quietly, full of discomfort and guilt, thinking I wasn't revolutionary enough. Later, when I was given a belt and told to whip an "enemy of the revolution", I ran away and was called a deserter by my fellow Red Guards. That same summer I caught a glimpse of Chairman Mao -- our Red Sun -- at Tiananmen Square, along with a million of other equally enthusiastic kids. I remember overwhelming feelings of joy. It wasn't until much later that I realized by blind idolization of Mao was a kind of worship even more fanatic than a cult. My father, a former war correspondent with state news agency Xinhua, was framed as a spy and denounced. But behind closed doors he warned my brother and I to "use our brains before taking action." "Don't do anything you will regret for the rest of your lives," he said. Slowly I began to Mao's wife Jiang Qing, who was a key leader of the Revolution, and I bowed grudgingly when my work unit had our mandatory daily worship ritual in front of the Chairman's image. 'Wolf's milk' My generation grew up drinking wolf's milk: we were born with hatred, and taught to struggle and hate everyone. Some of my fellow Red Guards argue that we were just innocent children led astray. But we were wrong. It pains me that many of my generation choose to forget the past and some even reminisce about the "good old days" when they could travel the country as privileged, carefree Red Guards. I do not confess because I committed fewer sins or experienced fewer hardships than others. I bear responsibility for many tragedies and abuses, and I can only express my regret to those who lost their loved ones during the Cultural Revolution. But I do not ask for forgiveness. I want to tell the truths of the Cultural Revolution as someone who lived through the madness and chaos, to warn people of the spectacular destructiveness, so that we can avoid ever repeating it. Fifty years on, however, I am worried by the increasing leader-worship we see in state media, similar to the ideological fervor that surrounded Mao. We must stay vigilant. We can't have the gruesome brutality of the Cultural Revolution start again. CNN's Katie Hunt in Hong Kong contributed to this report. TM &amp; © 2016 Cable News Network, Inc., a Time Warner Company. All rights reserved. """# let stringRange = NSRange(location: 0, length: testString.utf16.count) let matches = regex.matches(in: testString, range: stringRange) var result: [[String]] = [] for match in matches { var groups: [String] = [] for rangeIndex in 1 ..< match.numberOfRanges { let nsRange = match.range(at: rangeIndex) guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue } let string = (testString as NSString).substring(with: nsRange) groups.append(string) } if !groups.isEmpty { result.append(groups) } } print(result)

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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression