Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
d6b20dc5
Kaydet (Commit)
d6b20dc5
authored
Ara 06, 2007
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Better re.split examples.
üst
2b92f6ba
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
11 deletions
+17
-11
re.rst
Doc/library/re.rst
+17
-11
No files found.
Doc/library/re.rst
Dosyayı görüntüle @
d6b20dc5
...
@@ -1091,16 +1091,21 @@ method is invaluable for converting textual data into data structures that can b
...
@@ -1091,16 +1091,21 @@ method is invaluable for converting textual data into data structures that can b
easily read and modified by Python as demonstrated in the following example that
easily read and modified by Python as demonstrated in the following example that
creates a phonebook.
creates a phonebook.
First, get the input using triple-quoted string syntax::
First, here is the input. Normally it may come from a file, here we are using
triple-quoted string syntax::
>>> input = """Ross McFluff 834.345.1254 155 Elm Street
>>> input = """Ross McFluff: 834.345.1254 155 Elm Street
Ronald Heathmore 892.345.3428 436 Finley Avenue
Frank Burger 925.541.7625 662 South Dogwood Way
Heather Albrecht 548.326.4584 919 Park Place"""
Then, convert the string into a list with each line having its own entry::
Ronald Heathmore: 892.345.3428 436 Finley Avenue
Frank Burger: 925.541.7625 662 South Dogwood Way
>>> entries = re.split("\n", input)
Heather Albrecht: 548.326.4584 919 Park Place"""
The entries are separated by one or more newlines. Now we convert the string
into a list with each nonempty line having its own entry::
>>> entries = re.split("\n+", input)
>>> entries
>>> entries
['Ross McFluff 834.345.1254 155 Elm Street',
['Ross McFluff 834.345.1254 155 Elm Street',
'Ronald Heathmore 892.345.3428 436 Finley Avenue',
'Ronald Heathmore 892.345.3428 436 Finley Avenue',
...
@@ -1111,16 +1116,17 @@ Finally, split each entry into a list with first name, last name, telephone
...
@@ -1111,16 +1116,17 @@ Finally, split each entry into a list with first name, last name, telephone
number, and address. We use the ``maxsplit`` paramater of :func:`split`
number, and address. We use the ``maxsplit`` paramater of :func:`split`
because the address has spaces, our splitting pattern, in it::
because the address has spaces, our splitting pattern, in it::
>>> [re.split(" ", entry, 3) for entry in entries]
>>> [re.split("
:?
", entry, 3) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],
[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],
['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],
['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],
['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]
['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]
With a ``maxsplit`` of ``4``, we could seperate the house number from the street
The ``:?`` pattern matches the colon after the last name, so that it does not
name::
occur in the result list. With a ``maxsplit`` of ``4``, we could seperate the
house number from the street name::
>>> [re.split(" ", entry, 4) for entry in entries]
>>> [re.split("
:?
", entry, 4) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],
[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],
['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],
['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment