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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @/ (^|[.\?\!]\s+)(([A-Z][A-Za-z\'\-]+)\.?\s+)*([A-Z][A-Z\'\-]*\.?)(\s+[A-Z][A-Z\'\-]*\.?)*,?(\s+([A-z][A-z\'\-]+))*(\s*\([^\(\)]*\))?:(\s*\([^\(\)]*\))* /; string input = @"STEVE INSKEEP, host: It's MORNING EDITION from NPR News. I'm Steve Inskeep in Washington. RENEE MONTAGNE, host: And I'm Renee Montagne in London. INSKEEP: This week, NPR is beginning a yearlong series exploring the ways that people change the climate, and the way the climate changes people. MONTAGNE: Which is the reason I'm in Great Britain all this week. It's the first stop on our travels reporting on climate change. We're going around the world and making the occasional trip back in time. On this morning, I'm at London's Natural History Museum. Here you'll find the story of the some of the first people to come to Britain about 700,000 years ago. They faced some pretty dramatic changes in climate back then. NPR's Joe Palca has been learning how that might provide some clues for what lies ahead for us now, and he has this report. JOE PALCA: Whenever I go to Britain, I'm always impressed by how many old buildings and churches are still standing. My Scottish friend, Bruce, likes to remind me that there are toilets in Britain that are older than anything in America. But all this architectural history is a mere blink of an eye if you think in terms of Earth's climate history. SOUNDBITE OF MUSIC PALCA: The climate in Britain today is pleasant enough. But every hundred thousand years or so, it goes from pleasant to abominable. SOUNDBITE OF WIND BLOWING PALCA: It's a natural cycle caused by the way the Earth tilts and wobbles in its orbit around the sun. And when it's abominable, it's really abominable. Professor TONY STUART (Geology, University College London): You've got to imagine, if you flew over Britain at the time it would be like going over Greenland today or the Antarctic. It was covered with an ice sheet two or three miles thick. PALCA: That was geologist Tony Stuart. And when Britain is under an ice sheet, life grinds to a halt. Plants and animals, and people when they finally show up, have three basic choices: they find a way to adapt to living on a block of ice, they move out of the way, or they die. Eventually, the ice retreats. SOUNDBITE OF WATER DRIPPING Professor CLIVE GAMBLE (Geography, Royal Holloway, University of London): Once the ice sheets start to roll back, they go back fairly quickly. There's even a phrase for it, which is sort of instant deglacierization. PALCA: By instant, archaeologist Clive Gamble means geologic instant - centuries, or more like a millennia or two. And once it warms up, the plants and the animals and the people return. If you want to see this cycle of warm and cold, all you have to do is spend a few hours at a seaside town called West Runton. But be sure to take Tony Stuart with you if want to get a clear picture of how climate affects people and animals. Prof. STUART: It's a lovely day here. Really, we've been very, very lucky. And here is the North Sea. PALCA: The beach at West Runton is a narrow strip of sand with the North Sea on the left and a 40-foot cliff face on the right. Look up the cliff and you can see broad layers of various-colored sediments. Prof. STUART: So here you can already see the basic layers. PALCA: Each layer tells a dramatically different story. Prof. STUART: As you go down up the cliff, you go to the gray stuff, which is boulder clay laid down by an ice sheet. PALCA: But lower down. Prof. STUART: There's the black bed you see at the base of the cliff. It's very organic, packed with finely shredded plant material, bits of wood and seeds, and so on. PALCA: The black bed was laid down about 700,000 years ago, a time when things were reasonably warm in Britain. Prof. STUART: If you could go back in a time machine, you'd come out in the trees and the swamps and wet ground. And you'd think, oh, I've arrived in some reserve somewhere in the southern Britain. You wouldn't immediately realize there was anything wrong; it would all look very familiar. PALCA: Although, standing next to your time machine, you might notice something that wasn't so familiar. SOUNDBITE OF ELEPHANT PALCA: Yes, an elephant. There once were elephants in West Runton and most of the rest of Britain, for that matter. Prof. STUART: This pole here is where we excavated the (unintelligible). PALCA: And this elephant was huge, nearly twice as heavy as anything on Earth today. Prof. STUART: Really very spectacular finding, quite unique. PALCA: Why so big? Who knows for sure. But climate changes certainly helped shape the kinds of animals and plants and people wandering around on this planet. Okay, the plants didn't really wander. Now, so far we've been talking about dramatic climate variations. The kinds of variations that meant freezing to death or basking in a warm sun. But smaller changes, a degree here or there like we're already beginning to see today with global warming, can also have a big impact on over a far shorter span of time. How big and how fast is something scientists are trying to figure out? Let me take you over to Saham Toney, about 40 miles to the south. SOUNDBITE OF APPLAUSE PALCA: We're here at the ninth tee of the Richmond Park Golf Club in the town of Saham Toney. There's a small stream running along the course and on the other side, I see a large pit with a dozen or so men in rubber boots digging trenches. The trench diggers are actually scientists from some of Britain's top universities. Mr. NIGEL LARKIN (Curator of Geology, Norfolk Museum and Archeology Service): Should we wander down into the pit? PALCA: Nigel Larkin is with the local museum service. Mr. LARKIN: As you're walking, you can see the sediments changing under your feet. We're just going to cross some very sticky brown mud. You've now got orangey light brown gravel we're working on. PALCA: Nigel picks up a shovel and takes a slice out of the layer cake of sediment. Now each layer is only a few inches thick, and instead of seeing changes on a 100,000-year scale, we're seeing changes that took place in just a few thousands of years. Mr. LARKIN: This has just come out of the side of our trench. And all of these must be at least 50,000 years old here at this level. Oh, there's a seed. PALCA: Nigel says by studying the seeds and the pollen and the beetle remains... Mr. LARKIN: Beetles are very conservative in their habits. PALCA: ...they can see fine gradations in climate that are happening in between glaciers, when the climate is fairly stable. They've already discovered that the climate near Saham Toney was once less English countryside and more Mediterranean beach resort. As the global warming continues, it could be Mediterranean beach resort again. SOUNDBITE OF MUSIC PALCA: Back on our English beach resort at West Runton, I wanted to clear up something that's been bothering me ever since Tony Stuart told me about that West Runton elephant. Prof. STUART: We don't think of elephants living in a climate like this. I mean it's what, I don't know, probably in the forties today, or maybe even in the thirties. That seems a little chilly for the elephant. I mean, at least as we think of them today. Mr. LARKIN: There's an interesting question, because nowadays we associate elephants with warm, tropical parts of the world. In fact, if you look at things with a geological perspective, it's the modern world which is strange. Why aren't there any elephants running around in North America or in Europe today? It's our time which is the strange one. PALCA: And that's the point. We humans look at the world from a kind of a narrow perspective. We don't see elephants in Britain now so we assume they've never been here. We do see humans and we say, oh, they've always been here. In fact, neither is the case. We all just get our chance to play on Earth's stage when climate lets us, and then we're gone. You want proof? Visit Chris Stringer's office at London's Natural History Museum. Mr. CHRIS STRINGER (Paleontologist, London's Natural History Museum): Okay. So we've got a (unintelligible), and we've got a Neanderthal. PALCA: They didn't make it. Stringer's office is littered with bones and skulls of animals whose scenes have ended. Stringer runs a project that studies how climate and human migration interact. Mr. STRINGER: Each time it was warm, potentially Britain was a good place for people to be. Each time it was cold, Britain was a bad place for people to be. And as far as we know, in each of these peaks of these cold stages anyway, people probably disappeared from Britain completely. PALCA: Stringer says, basically, when the climate went bad, people had those three choices: adapt, move or die. It's a pattern of natural change that's been going on for hundreds of thousands of years. Mr. STRINGER: But now we're in a time when we're getting something that's not part of the cycle of natural change. So we're entering a new phase, and it certainly seems to me that this is now not a natural cycle, but something that is being produced by the action of one species - us. PALCA: Maybe humans have reached a point where we'll be able to adapt to whatever climate changes lie ahead. Maybe. It surely it won't take 100,000 years to get the answer. Joe Palca, NPR News. MONTAGNE: We leave London's Natural History Museum now. Tomorrow we'll go to the north of England; the unnatural cycle we're in now can be traced to events over 200 years ago in a small village there. SOUNDBITE OF MUSIC MONTAGNE: This is MORNING EDITION from NPR News."; RegexOptions options = RegexOptions.Multiline; foreach (Match m in Regex.Matches(input, pattern, options)) { Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index); } } }

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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx