Python RegEx
'n RegEx, of Regular Expression, is 'n reeks karakters wat 'n soekpatroon vorm.
RegEx kan gebruik word om te kyk of 'n string die gespesifiseerde soekpatroon bevat.
RegEx-module
Python het 'n ingeboude pakket genaamd re
, wat gebruik kan word om met gewone uitdrukkings te werk.
Voer die re
module in:
import re
RegEx in Python
Wanneer jy die re
module ingevoer het, kan jy gewone uitdrukkings begin gebruik:
Voorbeeld
Soek die string om te sien of dit met "Die" begin en met "Spanje" eindig:
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
RegEx-funksies
Die re
module bied 'n stel funksies wat ons in staat stel om 'n string vir 'n pasmaat te soek:
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
Meta-karakters
Metakarakters is karakters met 'n spesiale betekenis:
Character | Description | Example | Try it |
---|---|---|---|
[] | A set of characters | "[a-m]" | |
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | |
. | Any character (except newline character) | "he..o" | |
^ | Starts with | "^hello" | |
$ | Ends with | "planet$" | |
* | Zero or more occurrences | "he.*o" | |
+ | One or more occurrences | "he.+o" | |
? | Zero or one occurrences | "he.?o" | |
{} | Exactly the specified number of occurrences | "he{2}o" | |
| | Either or | "falls|stays" | |
() | Capture and group |
Spesiale reekse
'n Spesiale volgorde word \
gevolg deur een van die karakters in die lys hieronder, en het 'n spesiale betekenis:
Character | Description | Example | Try it |
---|---|---|---|
\A | Returns a match if the specified characters are at the beginning of the string | "\AThe" | |
\b | Returns a match where the specified characters are at the beginning or at the
end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\bain" r"ain\b" |
|
\B | Returns a match where the specified characters are present, but NOT at the beginning
(or at
the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\Bain" r"ain\B" |
|
\d | Returns a match where the string contains digits (numbers from 0-9) | "\d" | |
\D | Returns a match where the string DOES NOT contain digits | "\D" | |
\s | Returns a match where the string contains a white space character | "\s" | |
\S | Returns a match where the string DOES NOT contain a white space character | "\S" | |
\w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) | "\w" | |
\W | Returns a match where the string DOES NOT contain any word characters | "\W" | |
\Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
Stelle
'n Stel is 'n stel karakters binne 'n paar vierkantige hakies
[]
met 'n spesiale betekenis:
Set | Description | Try it |
---|---|---|
[arn] | Returns a match where one of the specified characters (a ,
r , or n ) are
present |
|
[a-n] | Returns a match for any lower case character, alphabetically between
a and n |
|
[^arn] | Returns a match for any character EXCEPT a ,
r , and n |
|
[0123] | Returns a match where any of the specified digits (0 ,
1 , 2 , or
3 ) are
present |
|
[0-9] | Returns a match for any digit between
0 and 9 |
|
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and
59 |
|
[a-zA-Z] | Returns a match for any character alphabetically between
a and z , lower case OR upper case |
|
[+] | In sets, + , * ,
. , | ,
() , $ ,{}
has no special meaning, so [+] means: return a match for any
+ character in the string |
Die findall()-funksie
Die findall()
funksie gee 'n lys terug wat alle passings bevat.
Voorbeeld
Druk 'n lys van alle wedstryde:
import re
txt = "The rain in Spain"
x = re.findall("ai",
txt)
print(x)
Die lys bevat die passings in die volgorde waarin hulle gevind word.
As geen passings gevind word nie, word 'n leë lys teruggestuur:
Voorbeeld
Gee 'n leë lys terug as geen passing gevind is nie:
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
Die soek() funksie
Die search()
funksie soek die string vir 'n passing, en gee 'n Match-objek terug as daar 'n passing is.
As daar meer as een wedstryd is, sal slegs die eerste keer van die wedstryd teruggestuur word:
Voorbeeld
Soek vir die eerste wit-spasie karakter in die string:
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
As geen passings gevind word nie, word die waarde None
teruggestuur:
Voorbeeld
Maak 'n soektog wat geen passing gee nie:
import re
txt = "The rain in Spain"
x = re.search("Portugal",
txt)
print(x)
Die split() funksie
Die split()
funksie gee 'n lys terug waar die string by elke passing verdeel is:
Voorbeeld
Verdeel by elke wit-spasie karakter:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt)
print(x)
U kan die aantal gevalle beheer deur die
maxsplit
parameter te spesifiseer:
Voorbeeld
Verdeel die string slegs by die eerste keer:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt,
1)
print(x)
Die sub() Funksie
Die sub()
funksie vervang die passings met die teks van jou keuse:
Voorbeeld
Vervang elke witspasie karakter met die nommer 9:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt)
print(x)
U kan die aantal vervangings beheer deur die
count
parameter te spesifiseer:
Voorbeeld
Vervang die eerste 2 gevalle:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt, 2)
print(x)
Pas voorwerp
'n Pasvoorwerp is 'n voorwerp wat inligting oor die soektog en die resultaat bevat.
Let wel: As daar geen passing is nie, None
sal die waarde teruggestuur word, in plaas van die Match Object.
Voorbeeld
Doen 'n soektog wat 'n Match Object sal gee:
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
Die Match-objek het eienskappe en metodes wat gebruik word om inligting oor die soektog te herwin, en die resultaat:
.span()
gee 'n tupel terug wat die begin- en eindposisies van die wedstryd bevat.
.string
gee die string terug wat in die funksie geslaag is
.group()
gee die deel van die string terug waar daar 'n passing was
Voorbeeld
Druk die posisie (begin- en eindposisie) van die eerste wedstrydvoorval.
Die gewone uitdrukking soek enige woorde wat met 'n hoofletter "S" begin:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
Voorbeeld
Druk die string wat in die funksie oorgedra is:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
Voorbeeld
Druk die deel van die tou waar daar 'n vuurhoutjie was.
Die gewone uitdrukking soek enige woorde wat met 'n hoofletter "S" begin:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
Let wel: As daar geen passing is nie, None
sal die waarde teruggestuur word, in plaas van die Match Object.