Tutorial Python

RUMAH Python Pengenalan Python Python Memulai Sintaks Python Komentar Python Variabel Python Tipe Data Python Nomor Python Pengecoran Python String Python Python Boolean Operator Python Daftar Python Tuple Python Set Python Kamus Python Python Jika...Lain Python Sementara Loop Python Untuk Loop Fungsi Python Python Lambda Array Python Kelas/Objek Python Warisan Python Python Iterator Lingkup Python Modul Python Tanggal Python Python Matematika Python JSON Python RegEx Python PIP Python Coba...Kecuali Masukan Pengguna Python Pemformatan String Python

Penanganan Berkas

Penanganan File Python File Baca Python Python Tulis/Buat File Python Hapus File

Modul Python

Tutorial NumPy Panduan Panda Tutorial sip

Python Matplotlib

Pengantar Matplotlib Matplotlib Memulai Matplotlib Pyplot Merencanakan Matplotlib Penanda Matplotlib Garis Matplotlib Label Matplotlib Kotak Matplotlib Subplot Matplotlib Penyebaran Matplotlib Matplotlib Bar Histogram Matplotlib Bagan Pai Matplotlib

Pembelajaran mesin

Mulai Mode Median Rata-rata Standar Deviasi Persentil Distribusi Data Distribusi Data Normal Plot Pencar Regresi linier Regresi Polinomial Regresi Berganda Skala Kereta/Tes Pohon Keputusan

Python MySQL

MySQL Memulai MySQL Buat Basis Data MySQL Buat Tabel Sisipan MySQL MySQL Pilih MySQL Dimana MySQL Dipesan Oleh Hapus MySQL Tabel Drop MySQL Pembaruan MySQL Batas MySQL MySQL Bergabung

Python MongoDB

MongoDB Memulai MongoDB Buat Basis Data MongoDB Buat Koleksi Sisipan MongoDB Temukan MongoDB Permintaan MongoDB Sortir MongoDB Hapus MongoDB Koleksi Jatuhkan MongoDB Pembaruan MongoDB Batas MongoDB

Referensi Python

Ikhtisar Python Fungsi bawaan Python Metode String Python Metode Daftar Python Metode Kamus Python Metode Tuple Python Metode Set Python Metode File Python Kata Kunci Python Pengecualian Python Daftar Istilah Python

Referensi Modul

Modul Acak Modul Permintaan Modul Statistik Modul Matematika Modul cMath

Python Bagaimana caranya?

Hapus Duplikat Daftar Membalikkan String Tambahkan Dua Angka

Contoh Python

Contoh Python Kompilator Python Latihan Python Kuis Python Sertifikat Python

Python RegEx


RegEx, atau Ekspresi Reguler, adalah urutan karakter yang membentuk pola pencarian.

RegEx dapat digunakan untuk memeriksa apakah string berisi pola pencarian yang ditentukan.


Modul RegEx

Python memiliki paket bawaan yang disebut re, yang dapat digunakan untuk bekerja dengan Ekspresi Reguler.

Impor remodul:

import re

RegEx dengan Python

Setelah Anda mengimpor remodul, Anda dapat mulai menggunakan ekspresi reguler:

Contoh

Cari string untuk melihat apakah itu dimulai dengan "The" dan diakhiri dengan "Spanyol":

import re

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

Fungsi RegEx

Modul reini menawarkan serangkaian fungsi yang memungkinkan kita mencari string untuk kecocokan:

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


Metakarakter

Metakarakter adalah karakter dengan arti khusus:

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    

Urutan Khusus

Urutan khusus \diikuti oleh salah satu karakter dalam daftar di bawah ini, dan memiliki arti khusus:

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

Himpunan adalah himpunan karakter di dalam sepasang tanda kurung siku []dengan arti khusus:

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

 

Fungsi findall()

Fungsi findall()mengembalikan daftar yang berisi semua kecocokan.

Contoh

Cetak daftar semua pertandingan:

import re

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

Daftar tersebut berisi kecocokan dalam urutan mereka ditemukan.

Jika tidak ada kecocokan yang ditemukan, daftar kosong akan dikembalikan:

Contoh

Kembalikan daftar kosong jika tidak ditemukan kecocokan:

import re

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

 

Fungsi pencarian ()

Fungsi search()mencari string untuk kecocokan, dan mengembalikan objek Match jika ada kecocokan.

Jika ada lebih dari satu kecocokan, hanya kemunculan pertama kecocokan yang akan dikembalikan:

Contoh

Cari karakter spasi putih pertama dalam 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())

Jika tidak ada kecocokan yang ditemukan, nilai Noneakan dikembalikan:

Contoh

Lakukan pencarian yang tidak menghasilkan kecocokan:

import re

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

 

Pemisahan () Fungsi

Fungsi split()mengembalikan daftar tempat string telah dipisah pada setiap kecocokan:

Contoh

Pisahkan di setiap karakter spasi putih:

import re

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

Anda dapat mengontrol jumlah kemunculan dengan menentukan maxsplit parameter:

Contoh

Pisahkan string hanya pada kemunculan pertama:

import re

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

 

Sub() Fungsi

Fungsinya sub()menggantikan kecocokan dengan teks pilihan Anda:

Contoh

Ganti setiap karakter spasi putih dengan angka 9:

import re

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

Anda dapat mengontrol jumlah penggantian dengan menentukan count parameter:

Contoh

Ganti 2 kejadian pertama:

import re

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

 

Cocokkan Obyek

Match Object adalah objek yang berisi informasi tentang pencarian dan hasilnya.

Catatan: Jika tidak ada yang cocok, nilai yang Noneakan dikembalikan, bukan Objek yang Cocok.

Contoh

Lakukan pencarian yang akan mengembalikan Objek yang Cocok:

import re

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

Objek Match memiliki properti dan metode yang digunakan untuk mengambil informasi tentang pencarian, dan hasilnya:

.span()mengembalikan Tuple yang berisi posisi awal, dan akhir pertandingan.
.stringmengembalikan string yang diteruskan ke fungsi
.group()mengembalikan bagian string yang cocok

Contoh

Cetak posisi (posisi awal dan akhir) dari kejadian kecocokan pertama.

Ekspresi reguler mencari kata apa pun yang dimulai dengan huruf besar "S":

import re

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

Contoh

Cetak string yang diteruskan ke fungsi:

import re

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

Contoh

Cetak bagian dari string di mana ada kecocokan.

Ekspresi reguler mencari kata apa pun yang dimulai dengan huruf besar "S":

import re

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

Catatan: Jika tidak ada yang cocok, nilai yang Noneakan dikembalikan, bukan Objek yang Cocok.