Python-Ref > Regular expressions (regexp) > Syntax
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Syntax

Basic components of a regular expression.
The following table summarizes the basic components of a regular expression. It is not complete, and is focused only on basics. The complete overview is part of the Python documentaion, please refer to it in case of unclarity.
Characters with special meaning in regular expressions have to be escaped if their literal meaning should be matched.
Sub-expression Example What it represents Usage
normal alphabet characters a,b,X, ,1 themselves "hi"=="hi"
. . any character "."=="a", "."=="X", "a."=="aB", "a."=="aa"
^ ^ beginning of a string
$ $ end of a string
* * zero or more repetitions of the preceding regexp "ab*"=="a", "ab*"=="ab", "ab*"=="abb", "ab*"!="ba"
+ + one or more repetitions of the preceding regexp "ab+"!="a", "ab+"=="ab", "ab+"=="abb", "ab+"!="ba"
? ? zero or one repetitions of the preceding regexp "ab?"=="a", "ab?"=="ab", "ab?"!="abb", "ab?"!="ba"
{x} {3} exactly x repetitions of the preceding regexp "ab{3}"!="a", "ab{3}"!="ab", "ab{3}"!="abb", "ab{3}"=="abbb"
{x,y} {1,3} between x and y repetitions of the preceding regexp "ab{1,3}"!="a", "ab{1,3}"=="ab", "ab{1,3}"=="abb", "ab{1,3}"=="abbb", "ab{1,3}"!="abbbb"
any repetition specifier + one extra ? *?,??,{1,4}? non-greedy version of the repetition specifier, matches the shortest possible string Greedy regexp
[] [abc][a-m] one of the characters inside brackets "[abc]"=="a", "[abc]=="c", "[abc]"!="ab"
[^] [^abc][^a-m] anything other than one of the characters inside brackets "[^abc]"!="a", "[^abc]!="c", "[^abc]"=="x"
| A|B,(dogs?)|(cats?) one of the separated regexps "A|B"=="A", "A|B"!="C", "(dogs?)|(cats?)"=="dog", "(dogs?)|(cats?)"=="cats", "(dogs?)|(cats?)"!="pig"
() ([a-z]+),(dogs?) creates one regexp from all the regexps in the brackets "pig?"=="pi", "pig?"=="pig", "(pig)?"=="pig", "(pig)?"!="pi", "(pig)?"==""
\d \d digit character, is equal to [0-9] for ascii "\d+"=="1", "\d+"=="1254", "\d+"!="12c"
\D \D non digit character, is equal to [^0-9] for ascii "\D+"=="a", "\D+"=="a b", "\D+"!="ab1"
\s \s any whitespace character, is equal to [ \t\n\r\f\v] for ascii "\s"=="\n", "\s"==" ", "\s"!="c"
\S \S any non-whitespace character, is equal to [^ \t\n\r\f\v] for ascii "\S"!="\n", "\S"!=" ", "\S"=="c"
\w \w any alphanumeric character, is equal to [a-zA-Z0-9_] for ascii "\w"=="a", "\w"=="8", "\w+"=="c_2", "\w+"!="c-2"
\W \W any non-alphanumeric character, is equal to [^a-zA-Z0-9_] for ascii "\W"!="a", "\W"!="8", "\W"=="-", "\W"!="!"