Python Eğitimi

Python ANA SAYFA Python Tanıtımı Python Başlarken Python Sözdizimi Python Yorumları Python Değişkenleri Python Veri Türleri Python Numaraları Python Döküm Python Dizeleri Python Boole'leri Python Operatörleri Python Listeleri piton demetleri Python Setleri Python Sözlükleri Python Eğer... Başka Python while Döngüleri Döngüler için Python Python Fonksiyonları piton lambda Python Dizileri Python Sınıfları/Nesneleri Python Kalıtımı Python Yineleyiciler Python Kapsamı Python Modülleri Python Tarihleri Python Matematik Python JSON'u Python Normal İfade Python PIP'si Python Deneyin... Hariç Python Kullanıcı Girişi Python Dize Biçimlendirme

Dosya yönetimi

Python Dosya İşleme Python Okuma Dosyaları Python Dosyaları Yazma/Oluşturma Python Dosyaları Sil

Python Modülleri

NumPy Eğitimi Panda İzlenecek Yol Scipy Eğitimi

Python Matplotlib'i

Matplotlib'e Giriş Matplotlib Başlarken Matplotlib Pyplot Matplotlib Çizimi Matplotlib İşaretçileri Matplotlib Hattı Matplotlib Etiketleri Matplotlib Izgarası Matplotlib Alt Noktaları Matplotlib Dağılımı Matplotlib Çubukları Matplotlib Histogramları Matplotlib Pasta Grafikleri

Makine öğrenme

Başlarken Ortalama Medyan Modu Standart sapma Yüzdelik Veri Dağıtımı Normal Veri Dağılımı Dağılım grafiği Doğrusal Regresyon Polinom Regresyon Çoklu regresyon Ölçek Tren/Test Karar ağacı

Python MySQL

MySQL'e Başlayın MySQL Veritabanı Oluştur MySQL Tablo Oluştur MySQL Ekle MySQL Seçimi MySQL Nerede MySQL Sıralaması MySQL Sil MySQL Bırakma Tablosu MySQL Güncellemesi MySQL Sınırı MySQL'e Katılma

Python MongoDB

MongoDB Başlarken MongoDB Veritabanı Oluştur MongoDB Koleksiyon Oluştur MongoDB Ekle MongoDB Bul MongoDB Sorgusu MongoDB Sıralaması MongoDB Sil MongoDB Damla Koleksiyonu MongoDB Güncellemesi MongoDB Sınırı

Python Referansı

Python'a Genel Bakış Python Yerleşik İşlevleri Python Dizisi Yöntemleri Python Liste Yöntemleri Python Sözlük Yöntemleri Python Tuple Yöntemleri Python Kümesi Yöntemleri Python Dosya Yöntemleri Python Anahtar Kelimeleri Python İstisnaları Python Sözlüğü

Modül Referansı

Rastgele Modül İstek Modülü İstatistik Modülü Matematik Modülü cMath Modülü

Python Nasıl Yapılır?

Liste Kopyalarını Kaldır Bir Dizeyi Ters Çevir İki Sayı Ekle

Python Örnekleri

Python Örnekleri Python Derleyici Python Alıştırmaları Python Testi Python Sertifikası

Python Normal İfade


Bir RegEx veya Normal İfade, bir arama kalıbı oluşturan bir karakter dizisidir.

RegEx, bir dizenin belirtilen arama modelini içerip içermediğini kontrol etmek için kullanılabilir.


Normal İfade Modülü

rePython, Normal İfadelerle çalışmak için kullanılabilecek yerleşik bir pakete sahiptir .

reModülü içe aktarın :

import re

Python'da RegEx

Modülü içe reaktardığınızda, normal ifadeleri kullanmaya başlayabilirsiniz:

Örnek

"The" ile başlayıp "İspanya" ile bitip bitmediğini görmek için dizeyi arayın:

import re

txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)

Normal İfade İşlevleri

Modül re, bir eşleşme için bir dizi aramamıza izin veren bir dizi işlev sunar:

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 karakterler

Meta karakterler, özel bir anlamı olan karakterlerdir:

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    

Özel Diziler

Özel bir dizi, \aşağıdaki listedeki karakterlerden birinin ardından gelir ve özel bir anlamı vardır:

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"

Setler

[]Küme, bir çift köşeli parantez içindeki özel bir anlamı olan bir karakter kümesidir :

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() İşlevi

İşlev findall(), tüm eşleşmeleri içeren bir liste döndürür.

Örnek

Tüm eşleşmelerin bir listesini yazdırın:

import re

txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)

Liste, eşleşmeleri bulundukları sırayla içerir.

Eşleşme bulunamazsa boş bir liste döndürülür:

Örnek

Eşleşme bulunamazsa boş bir liste döndür:

import re

txt = "The rain in Spain"
x = re.findall("Portugal", txt)
print(x)

 

arama() Fonksiyonu

İşlev search(), dizede eşleşme arar ve eşleşme varsa Match nesnesini döndürür.

Birden fazla eşleşme varsa, eşleşmenin yalnızca ilk geçtiği yer döndürülür:

Örnek

Dizedeki ilk boşluk karakterini arayın:

import re

txt = "The rain in Spain"
x = re.search("\s", txt)

print("The first white-space character is located in position:", x.start())

Eşleşme bulunamazsa, değer Nonedöndürülür:

Örnek

Eşleşme döndürmeyen bir arama yapın:

import re

txt = "The rain in Spain"
x = re.search("Portugal", txt)
print(x)

 

split() Fonksiyonu

İşlev split(), dizenin her eşleşmede bölündüğü bir liste döndürür:

Örnek

Her beyaz boşluk karakterinde bölün:

import re

txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)

maxsplit Parametreyi belirterek oluşum sayısını kontrol edebilirsiniz :

Örnek

Dizeyi yalnızca ilk oluşumda bölün:

import re

txt = "The rain in Spain"
x = re.split("\s", txt, 1)
print(x)

 

alt() İşlevi

İşlev sub(), eşleşmeleri seçtiğiniz metinle değiştirir:

Örnek

Her boşluk karakterini 9 sayısıyla değiştirin:

import re

txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)

Parametreyi belirterek değiştirme sayısını kontrol edebilirsiniz count :

Örnek

İlk 2 oluşumu değiştirin:

import re

txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)

 

Nesneyi Eşleştir

Eşleşme Nesnesi, arama ve sonuç hakkında bilgi içeren bir nesnedir.

Not: Eşleşme yoksa, Eşleşme Nesnesi yerine değer Nonedöndürülür.

Örnek

Bir Eşleşme Nesnesi döndürecek bir arama yapın:

import re

txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an object

Match nesnesi, arama ve sonuç hakkında bilgi almak için kullanılan özelliklere ve yöntemlere sahiptir:

.span()eşleşmenin başlangıç ​​ve bitiş konumlarını içeren bir demet döndürür.
.stringişleve iletilen dizeyi döndürür, dizenin
.group()eşleşme olan bölümünü döndürür

Örnek

İlk eşleşme oluşumunun konumunu (başlangıç ​​ve bitiş konumu) yazdırın.

Normal ifade, büyük harf "S" ile başlayan tüm kelimeleri arar:

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())

Örnek

İşleve iletilen dizeyi yazdırın:

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)

Örnek

Dizenin eşleşme olan kısmını yazdırın.

Normal ifade, büyük harf "S" ile başlayan tüm kelimeleri arar:

import re

txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())

Not: Eşleşme yoksa, Eşleşme Nesnesi yerine değer Nonedöndürülür.