لغة Python RegEx
التعبير العادي ، أو التعبير العادي ، هو سلسلة من الأحرف التي تشكل نمط بحث.
يمكن استخدام RegEx للتحقق مما إذا كانت السلسلة تحتوي على نمط البحث المحدد.
وحدة RegEx
تحتوي Python على حزمة مضمنة تسمى re
، والتي يمكن استخدامها للعمل مع التعبيرات العادية.
قم باستيراد re
الوحدة:
import re
RegEx في Python
عندما تقوم باستيراد re
الوحدة ، يمكنك البدء في استخدام التعبيرات العادية:
مثال
ابحث عن السلسلة لمعرفة ما إذا كانت تبدأ بـ "The" وتنتهي بـ "Spain":
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
وظائف RegEx
تقدم re
الوحدة مجموعة من الوظائف التي تسمح لنا بالبحث في سلسلة عن تطابق:
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 |
الحروف الأولية
الحروف الأولية هي أحرف لها معنى خاص:
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 |
التسلسلات الخاصة
التسلسل الخاص هو \
متبوع بأحد الرموز الموجودة في القائمة أدناه ، وله معنى خاص:
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" |
مجموعات
المجموعة هي مجموعة من الأحرف داخل زوج من الأقواس المربعة
[]
ذات معنى خاص:
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 |
دالة findall ()
تقوم findall()
الوظيفة بإرجاع قائمة تحتوي على جميع التطابقات.
مثال
اطبع قائمة بجميع التطابقات:
import re
txt = "The rain in Spain"
x = re.findall("ai",
txt)
print(x)
تحتوي القائمة على التطابقات بالترتيب الذي تم العثور عليها فيه.
إذا لم يتم العثور على مطابقات ، يتم إرجاع قائمة فارغة:
مثال
قم بإرجاع قائمة فارغة إذا لم يتم العثور على تطابق:
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
وظيفة البحث ()
تبحث الوظيفة search()
في السلسلة عن تطابق ، وترجع كائن Match إذا كان هناك تطابق.
إذا كان هناك أكثر من تطابق ، فسيتم إرجاع المرة الأولى فقط من المباراة:
مثال
ابحث عن أول حرف مسافة بيضاء في السلسلة:
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
إذا لم يتم العثور على مطابقات ، None
يتم إرجاع القيمة:
مثال
قم بإجراء بحث لا يُرجع أي تطابق:
import re
txt = "The rain in Spain"
x = re.search("Portugal",
txt)
print(x)
وظيفة الانقسام
تقوم split()
الدالة بإرجاع قائمة حيث تم تقسيم السلسلة عند كل مباراة:
مثال
انقسام عند كل حرف مسافة بيضاء:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt)
print(x)
يمكنك التحكم في عدد التكرارات عن طريق تحديد
maxsplit
المعلمة:
مثال
قم بتقسيم السلسلة عند التكرار الأول فقط:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt,
1)
print(x)
الوظيفة الفرعية ()
تستبدل الوظيفة sub()
التطابقات بالنص الذي تختاره:
مثال
استبدل كل حرف مسافة بيضاء بالرقم 9:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt)
print(x)
يمكنك التحكم في عدد الاستبدالات من خلال تحديد
count
المعلمة:
مثال
استبدل أول مرتين:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt, 2)
print(x)
كائن المباراة
كائن المطابقة هو كائن يحتوي على معلومات حول البحث والنتيجة.
ملاحظة: إذا لم يكن هناك تطابق ، None
فسيتم إرجاع القيمة بدلاً من كائن المطابقة.
مثال
قم ببحث يعيد كائن مطابق:
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
كائن Match له خصائص وطرق مستخدمة لاسترداد معلومات حول البحث ، والنتيجة:
.span()
تُرجع مجموعة تحتوي على مواضع البداية والنهاية للمباراة.
.string
تُرجع السلسلة التي تم تمريرها إلى الدالة
.group()
الجزء الذي يوجد به تطابق من السلسلة
مثال
اطبع الموضع (موضع البداية والنهاية) عند حدوث المباراة الأولى.
يبحث التعبير النمطي عن أي كلمات تبدأ بحرف كبير "S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
مثال
اطبع السلسلة التي تم تمريرها إلى الوظيفة:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
مثال
اطبع الجزء الذي يوجد به تطابق من السلسلة.
يبحث التعبير النمطي عن أي كلمات تبدأ بحرف كبير "S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
ملاحظة: إذا لم يكن هناك تطابق ، None
فسيتم إرجاع القيمة بدلاً من كائن المطابقة.