Regular expression facts for kids
A regular expression (often called regex or regexp) is like a special code or pattern that computers use to find and match specific text. Think of it as a super-smart search tool! Many programming languages use these patterns. A special program or part of a programming language uses the regular expression to find what you are looking for in a text.
This special program, sometimes called a regular expression processor, helps the computer understand the pattern you've given it. It then checks a text to see if it matches that pattern.
Here are some examples of what you can find using regular expressions:
- The letters "car" appearing one after another, like in "car", "cartoon", or "bicarbonate".
- The letters "car" in that order, even if other letters are between them, like in "Icelander" or "chandler".
- The word "car" only when it stands alone.
- The word "car" when it comes right after the word "blue" or "red".
- The word "car" when it does not come after the word "motor".
- A dollar sign ($) followed by one or more numbers, and then maybe a period (.) and exactly two more numbers (for example, "$10" or "$245.99"). This pattern would not find "$ 5" because of the space, or "€25" because it doesn't have a dollar sign.
Regular expressions can be much more complicated than these simple examples. Many regular expression languages also use "wildcard" characters. These are special symbols that can stand for "any character" or "any number of characters," making patterns even more flexible.

Contents
How Do Regular Expressions Work?
Regular expressions use a set of rules to define a pattern. When you give a computer a regex, it uses these rules to scan through text. It checks each part of the text to see if it fits the pattern you described. If it finds a match, it can then do something with that text, like highlight it, replace it, or just tell you it found it.
For example, if you want to find all phone numbers in a document, you could write a regular expression that describes what a phone number looks like (e.g., three numbers, a dash, three numbers, a dash, four numbers). The computer would then use that pattern to find every phone number in the document.
Finding Text with Regex Patterns
Regular expressions are very powerful because they let you be very specific about what you want to find.
Simple Text Matches
You can search for a simple sequence of letters. For example, if you search for the pattern "cat", it will find "cat" in words like "cat", "catsup", or "concatenate". It's like a normal search, but it's just the beginning of what regex can do.
Matching Whole Words
Sometimes you only want to find a word when it's by itself, not as part of a bigger word. For instance, if you search for the word "run", you might not want to find "running" or "runner". Regular expressions have special symbols that let you say "match this word only if it's a whole word."
Looking for Specific Order
You can also look for words that appear in a certain order or have other words around them. For example, you could find "apple" only if it's followed by "pie". Or you could find "dog" only if it's not followed by "house". This helps you be very precise in your searches.
Finding Money Amounts
Regular expressions are great for finding specific formats, like money. Imagine you want to find all prices in a document. You could create a pattern that looks for a dollar sign, then some numbers, and then optionally a period and two more numbers for cents. This pattern would correctly find "$5", "$12.50", and "$1000.00", but it would ignore things like "5 dollars" or "€10".
What Are Wildcard Characters?
Wildcard characters are special symbols in regular expressions that stand for other characters. For example, one common wildcard might mean "any single character." Another might mean "zero or more of the previous character." These wildcards make your patterns much more flexible, allowing you to match a wider range of text with a single regex.
Images for kids
-
A blacklist on Wikipedia uses regular expressions to stop bad page titles.
See also
In Spanish: Expresión regular para niños