pacman, rainbows, and roller s
HomeBlogAbout Me

Expressions 1 3 – Play With Regular Expressions



Lesson 1: An Introduction, and the ABCsLesson 1½: The 123sLesson 2: The DotLesson 3: Matching specific charactersLesson 4: Excluding specific charactersLesson 5: Character rangesLesson 6: Catching some zzz'sLesson 7: Mr. Kleene, Mr. KleeneLesson 8: Characters optionalLesson 9: All this whitespaceLesson 10: Starting and endingLesson 11: Match groupsLesson 12: Nested groupsLesson 13: More group workLesson 14: It's all conditionalLesson 15: Other special charactersLesson X: Infinity and beyond!

The duplicate closing reason is incorrect, but yes, this regex doesn't make sense. The character group includes a dash, a word character, a digit (which generally is already a word character), an opening brace, digits 1 to 3 (inclusive), and a closing brace.

Practice Problems
  • 1.9: no: ECMA 1.42–1.73: no: no: no: no: no: no: no: no: Greedy quantifier. (star) Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all. ' matches 'def' 'ghi' in.
  • Regular expressions (shortened as 'regex') are special strings representing a pattern to be matched in a search operation. They are an important tool in a wide variety of computing applications, from programming languages like Java and Perl, to text processing tools like grep, sed, and the text editor vim.
Problem 1: Matching a decimal numbersProblem 2: Matching phone numbersProblem 3: Matching emailsProblem 4: Matching HTMLProblem 5: Matching specific filenamesProblem 6: Trimming whitespace from start and end of lineProblem 7: Extracting information from a log fileProblem 8: Parsing and extracting data from a URLProblem X: Infinity and beyond!

Regular expressions are extremely useful in extracting information from text such as code, log files, spreadsheets, or even documents. And while there is a lot of theory behind formal languages, the following lessons and examples will explore the more practical uses of regular expressions so that you can use them as quickly as possible.

The first thing to recognize when using regular expressions is that everything is essentially a character, and we are writing patterns to match a specific sequence of characters (also knownas a string). Most patterns use normal ASCII, which includes letters, digits, punctuation and other symbols on your keyboard like %#$@!, but unicode characters can also be used to match any type ofinternational text. Invaders return from the planet moolah slot machine.

Below are a couple lines of text, notice how the text changes to highlight the matching characterson each line as you type in the input field below. To continue to the next lesson, you will needto use the new syntax and concept introduced in each lesson to write a pattern that matches all thelines provided.

Go ahead and try writing a pattern that matches all three rows, it may be as simple as the commonletters on each line.

TaskText
matchabcdefg
matchabcde
matchabc
Solution

Try typing the first three characters abc to see them match all the lines.

Solve the above task to continue on to the next problem, or read the .
Find RegexOne useful? Please consider
to support our site.
abc…Letters
123…Digits
dAny Digit
DAny Non-digit character
.Any Character
.Period
[abc]Only a, b, or c
[^abc]Not a, b, nor c
[a-z]Characters a to z
[0-9]Numbers 0 to 9
wAny Alphanumeric character
WAny Non-alphanumeric character
{m}m Repetitions
{m,n}m to n Repetitions
*Zero or more repetitions
+One or more repetitions
?Optional character
sAny Whitespace
SAny Non-whitespace character
^…$Starts and ends
(…)Capture Group
(a(bc))Capture Sub-group
(.*)Capture all
(abc|def)Matches abc or def

Regular expressions are a powerful tool for finding and replacing text in a program, or at the command line. This document describes the most common regular expression symbols, and how to use them.

Description

Regular expressions (shortened as 'regex') are special strings representing a pattern to be matched in a search operation. They are an important tool in a wide variety of computing applications, from programming languages like Java and Perl, to text processing tools like grep, sed, and the text editor vim. Below is an example of a regular expression.

The power of regular expressions comes from its use of metacharacters, which are special characters (or sequences of characters) that are used to represent something else. For instance, in a regular expression the metacharacter ^ means 'not'. So, while 'a' means 'match lowercase a', '^a' means 'do not match lowercase a'.

The tables below describe many standard components of regular expressions.

Note

There are different so-called 'flavors' of regex — Java, Perl, and Python have slightly different rules for regular expressions, for example. On this page, we stick to standard regex, and you should be able to use this reference for any implementation.

Anchors and Boundaries

Anchors and boundaries allow you to describe text in terms of where it's located. For instance, you might want to search for a certain word, but only if it's the first word on a line. Or you might want to look for a certain series of letters, but only if they appear at the very end of a word.

Metacharacter SequenceMeaningExample ExpressionExample Match
^Start of string or line^abcabc (appearing at start of string or line)
$End of string, or end of linexyz$xyz (appearing at end of string or line)
bWord boundaryingbmatching (matches ing if it is at the end of a word)
BNOT word-boundaryBingBstinger (matches ing if it is not at the beginning or end of the word)
<Start of word<whenwhenever (matches when only if it is at the beginning of a word)
>End of wordever>whenever (matches ever only if it is at the end of a word)

Character Classes

When searching for text, it's useful to be able to choose characters based solely upon their classification. The fundamental classes of character are 'word' characters (such as numbers and letters) and 'non-word' characters (such as spaces and punctuation marks).

Metacharacter SequenceMeaningExample ExpressionExample Match
.Matches any single character except the newline character.ab.defabcdef, ab9def, ab=def
sMatches a whitespace character (such as a space, a tab, a form feed, etc.)abcdseabcd e, abcd(tab)e
SNOT whitespaceSSsSAB D, 99(tab)9
wA word character. A word character is a letter, a number, or an underscore. This set of characters may also be represented by the regex character set[a-zA-Z0-9_]w{1,}-w{1,}
(see quantifiers, below)
well-wishes, far-fetched
WNOT a word characterwW{1,}wa,!-(?&;b, 9-5

Special Whitespace Characters

Metacharacter SequenceMatches
na newline
ta tab
ra carriage return
va vertical tab
fa form feed

Quantifiers

Quantifiers allow you to declare quantities of data as part of your pattern. For instance, you might need to match exactly six spaces, or locate every numeric string that is between four and eight digits in length.

Metacharacter SequenceMeaningExample ExpressionExample Match
*Zero or more of the preceding characterca*tcat, ct, caaaaaaaaaaaat
character{m}Exactly m occurrences of characterf{3}fff
character{m,n}No fewer than m, but no more than n occurrences of characterg{4,6}gggg, ggggg, gggggg
character{m,}At least m occurrences of characterh{2,}hh, hhhhhhhh, and hhhhhhhhhhhhhh would match, but h would not

Literal Characters and Sequences

Metacharacters are a powerful tool because they have special meaning, but sometimes they need to be matched literally. Msi international. For instance, you might need to search for a literal dollar sign ('$') as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means 'end of line' in regex, you must escape it with a backslash to use it literally.

Metacharacter SequenceMeaningExample ExpressionExample Match
Literal backslash
^Literal caret^{5}^^^^^
$Literal dollar sign$5$5
.Literal periodYes.Yes.
*Literal asterisktypo*typo*
[Literal open bracket[3[]3, [
]Literal close bracket]]

Character Sets and Ranges

Lucky charms casino games. A character set is an explicit list of the characters that may qualify for a match in a search. A character set is indicated by enclosing a set of characters in brackets ([ and ]). For instance, the character set [abz] will match any of the characters a, b, or z, or a combination of these such as ab, za, or baz.

Ranges are a type of character set which uses a dash in between characters to imply the entire range of characters between them, as well as the beginning and end characters themselves. For instance, the range [e-h] would match any of the characters e, f, g, or h, or any combination of these, such as hef. The range [3-5] would match any of the digits 3, 4, or 5, or a combination of these such as 45. Aiseesoft mxf converter 9 1 8.

When defining a range of characters, you can figure out the exact order in which they appear by looking at an ASCII character table. Live desktop 8 0 x.

Expressions 1 3 – Play With Regular Expressions
Metacharacter SequenceMeaningExample ExpressionExample Match
[characters]The characters listed inside the brackets are part of a matching-character set[abcd]a, b, c, d, abcd
[^.]Characters inside the brackets are a NON-matching set. Any character not inside the brackets is a matching character.[^abcd]Any occurrence of any character EXCEPT a, b, c, d. For instance, when, zephyr, e, xyz
[character-character]Any character in the range between two characters, including the characters, is part of the set[a-z]Any lowercase letter
[^character]Any character that is NOT the listed character[^A]Any character EXCEPT capital A
Ranges can also be combined by concatenating. For instance:[f-hAC-E3-5]Matches any occurrence of an f, g, h, A, C, D, E, 3, 4, 5
Ranges can also be modified with a quantifier. For instance:[a-c0-2]*Matches zero or more consecutive occurrences of a, b, c, 0, 1, 2. For example, ac1cb would match

Expressions 1 3 – Play With Regular Expressions Worksheets

Grouping

Grouping allows you to treat another expression as a single unit.

Metacharacter SequenceMeaningExample ExpressionExample Match
(expression)expression will match as a group(ab)ab, abracadabra
Grouped expressions can be treated as a unit just like a character. For example:(ab){3}abababcdefg

Extended Regular Expressions

Extended Regular Expressions, or ERE, are an extension of basic regular expressions.

EREs support additional quantifiers, do not require certain metacharacters to be escaped, and obey other special rules. If your application supports extended regex, consult your manual for their proper syntax.

Related Pages

Expressions 1 3 – Play With Regular Expressions Worksheet

grep — Command-line tool for finding text which matches a regular expression.
sed — Command-line tool for filtering and transforming text using regular expressions.
vim — A powerful text editor.





Expressions 1 3 – Play With Regular Expressions
Back to posts
This post has no comments - be the first one!

UNDER MAINTENANCE