Skip to content

Commit d991548

Browse files
author
Saeid Darvish
committed
l16: findall, finiter
1 parent aa1c6a1 commit d991548

1 file changed

Lines changed: 132 additions & 1 deletion

File tree

lessons/l16.rst

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929

30-
توابع جستجو
30+
توابع جستجو، ماژول ``re`` پایتون
3131
---------------------------------------
3232

3333
توابع پرکاربرد ماژول ``re`` پایتون مرتبط با عمل جستجو در یک متن عبارتند از:
@@ -460,6 +460,137 @@
460460
>>> match = re.fullmatch('Py...n', 'Python')
461461

462462

463+
تابع ``findall``
464+
~~~~~~~~~~~~~~~~~~~~~~
465+
466+
467+
``findall(pattern, string, flags=0)``
468+
469+
این تابع (``findall``) حاصل تمام انطباق‌های ممکن pattern در string را در قالب یک لیست از رشته‌ها (نتایج) برمی‌گرداند [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.findall>`__]::
470+
471+
>>> import re
472+
473+
>>> string = "My number is 123456789 and my friend's number is 987654321"
474+
>>> results = re.findall(r'\d+', string)
475+
476+
>>> type(results)
477+
<class 'list'>
478+
479+
>>> print(results)
480+
['123456789', '987654321']
481+
482+
تابع ``findall`` از سمت چپ string شروع به دنبال انطباق pattern در آن می‌گردد و نتایج را به ترتیب برمی‌گرداند. اگر الگو (pattern) شامل گروه باشد فقط نتایج مربوط به انطباق گروه را برمی‌گرداند و نه تمام الگو را::
483+
484+
>>> results = re.findall(r'#(\w+)#', '#Perl#.#Python#.#Ruby#')
485+
>>> print(results)
486+
['Perl', 'Python', 'Ruby']
487+
488+
>>> results = re.findall(r'#\w+#', '#Perl#.#Python#.#Ruby#')
489+
>>> print(results)
490+
['#Perl#', '#Python#', '#Ruby#']
491+
492+
چنانچه الگو شامل بیش از یک گروه باشد، خروجی تابع ``findall`` برابر است با یک لیست از تاپل‌ها که هر تاپل، حاصل یک دور انطباق است::
493+
494+
>>> results = re.findall(r'(\w+)@(\d+)', 'Perl@1987,Python@1991,Ruby@1995')
495+
>>> print(results)
496+
[('Perl', '1987'), ('Python', '1991'), ('Ruby', '1995')]
497+
498+
499+
یادآوری می‌شود که دو نمونه کد زیر عملکردی معادل یکدیگر دارند::
500+
501+
502+
>>> pattern = re.compile('Py...n')
503+
>>> results = pattern.findall('PythonPythonPython')
504+
505+
::
506+
507+
>>> results = re.findall('Py...n', 'PythonPythonPython')
508+
509+
510+
تابع ``finditer``
511+
~~~~~~~~~~~~~~~~~~~~~~
512+
513+
514+
``finditer(pattern, string, flags=0)``
515+
516+
خروجی این تابع (``finditer``) یک شی ``iterator`` (شی تکرارکننده - درس نهم) است و حاصل هر بار پیمایش آن یک شی ``Match`` می‌باشد که همانند تابع ``findall`` از سمت چپ string شروع به دنبال انطباق pattern در آن می‌گردد و نتایج را به ترتیب برمی‌گرداند. [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.finditer>`__]::
517+
518+
>>> import re # Python 3.x
519+
520+
>>> string = "My number is 123456789 and my friend's number is 987654321"
521+
>>> result = re.finditer(r'\d+', string)
522+
523+
>>> type(result)
524+
<class 'callable_iterator'>
525+
526+
>>> result.__next__()
527+
<re.Match object; span=(13, 22), match='123456789'>
528+
529+
>>> result.__next__()
530+
<re.Match object; span=(49, 58), match='987654321'>
531+
532+
>>> result.__next__()
533+
Traceback (most recent call last):
534+
File "<stdin>", line 1, in <module>
535+
StopIteration
536+
537+
::
538+
539+
>>> for match in re.finditer(r'#(\w+)#', '#Perl#.#Python#.#Ruby#'):
540+
... print(match)
541+
...
542+
<re.Match object; span=(0, 6), match='#Perl#'>
543+
<re.Match object; span=(7, 15), match='#Python#'>
544+
<re.Match object; span=(16, 22), match='#Ruby#'>
545+
546+
547+
>>> for match in re.finditer(r'#\w+#', '#Perl#.#Python#.#Ruby#'):
548+
... print(match)
549+
...
550+
<re.Match object; span=(0, 6), match='#Perl#'>
551+
<re.Match object; span=(7, 15), match='#Python#'>
552+
<re.Match object; span=(16, 22), match='#Ruby#'>
553+
554+
::
555+
556+
>>> for match in re.finditer(r'(\w+)@(\d+)', 'Perl@1987,Python@1991,Ruby@1995'):
557+
... print(match)
558+
...
559+
<re.Match object; span=(0, 9), match='Perl@1987'>
560+
<re.Match object; span=(10, 21), match='Python@1991'>
561+
<re.Match object; span=(22, 31), match='Ruby@1995'>
562+
563+
564+
یادآوری می‌شود که دو نمونه کد زیر عملکردی معادل یکدیگر دارند::
565+
566+
567+
>>> pattern = re.compile('Py...n')
568+
>>> result = pattern.finditer('PythonPythonPython')
569+
570+
::
571+
572+
>>> result = re.finditer('Py...n', 'PythonPythonPython')
573+
574+
575+
576+
توابع جایگزینی، ماژول ``re`` پایتون
577+
---------------------------------------
578+
579+
توابع پرکاربرد ماژول ``re`` پایتون مرتبط با عمل جایگزینی (replace) یک متن عبارتند از:
580+
581+
* ``sub``
582+
* ``subn``
583+
584+
585+
586+
تابع ``sub``
587+
~~~~~~~~~~~~~~~~~~~~~~
588+
589+
590+
``sub(pattern, repl, string, count=0, flags=0)``
591+
592+
593+
463594

464595
|
465596

0 commit comments

Comments
 (0)