Skip to content

Commit e0eb4b1

Browse files
author
Saeid Darvish
authored
Merge pull request #13 from saeiddrv/draft
add GitHub button
2 parents 3c98556 + 1faa81f commit e0eb4b1

3 files changed

Lines changed: 174 additions & 2 deletions

File tree

_templates/sphinx_minoo_theme/includes/header.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@
9999

100100
<script type="text/javascript" src="{{ pathto('_static/minoo.js', 1) }}"></script>
101101
<script type="text/javascript" src="{{ pathto('_static/modernizr.min.js', 1) }}"></script>
102+
103+
<script async defer src="https://buttons.github.io/buttons.js"></script>

_templates/sphinx_minoo_theme/includes/top.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
</a>
4040
</div>
4141

42+
<div class="top-links" style="direction: ltr;float: right;">
43+
<a class="github-button" href="https://github.com/saeiddrv/PythonPersianTutorial" data-size="large" data-show-count="true" aria-label="Star saeiddrv/PythonPersianTutorial on GitHub">Star</a>
44+
<div>
45+
4246
<div class="top-links top-blue">
4347

4448
</div>

lessons/l16.rst

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112

113113
این متد از شی ``Match``، گروه (های) تطبیق داده شده بر اساس الگو مورد نظر را برمی‌گرداند. این متد می‌تواند یک یا چند آرگومان عددی دریافت کند که معرف اندیس گروه مورد نظر می‌باشد. در حالت فراخوانی بدون آرگومان تمامی گروه‌های تطبیق داده شده به صورت یک مقدار رشته برگردانده می‌شود و در صورتی تنها یک مقدار به آن ارسال گردد، گروه تطبیق داده شده متناظر با آن اندیس (شمارش اندیس‌ها از یک است) در قالب یک شی رشته برگردانده می‌شود و در صورتی که بیش از یک اندیس به عنوان آرگومان ارسال گردد یک شی تاپل محتوی گروه‌های تطبیق داده شده برگردانده خواهد شد. چنانچه آرگومان ارسالی عددی منفی باشد یا اندیسی بالاتر از تعداد گروه‌های تطبیق داده شده باشد، یک استثنا ``IndexError`` رخ خواهد داد::
114114

115-
>>> match = re.search(r"(\w+) (\w+)", "Isaac Newton, physicist")
115+
>>> match = re.search("(\w+) (\w+)", "Isaac Newton, physicist")
116116
>>> match.group() # The entire match
117117
'Isaac Newton'
118118
>>> match.group(0) # The entire match
@@ -181,7 +181,7 @@
181181

182182
>>> import re
183183

184-
>>> match = re.search(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Elvis Presley")
184+
>>> match = re.search("(?P<first_name>\w+) (?P<last_name>\w+)", "Elvis Presley")
185185
>>> match.groupdict()
186186
{'first_name': 'Elvis', 'last_name': 'Presley'}
187187

@@ -212,6 +212,59 @@
212212

213213

214214

215+
* ``Match.expand(template)`` [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.Match.expand>`__]
216+
217+
این متد حاصل انطباق را در قالب ``template`` جایگذاری کرده و یک شی رشته جدید برمی‌گرداند. قالب در اینجا یک رشته است که در آن می‌توان به حاصل انطباق گروهبندی‌های موجود در الگو ارجاع داد به این صورت کرد که می‌توان گروه‌های بی‌نام موجود در الگو را با استفاده از ارجاع عددی به شماره اندیس آن‌ها به مانند ``1\``، ``2\`` و گروه‌های بانام را با استفاده از نام گروه به مانند ``<g<name\`` یا ``<g<1\`` در قالب موجود جایگذاری کرد. Escape character‌های موجود در قالب به کاراکترهای مناسب خود در متن خروجی تبدیل می‌شوند و از **نسخه 3.5 پایتون** گروه‌هایی که هیچ انطباقی نداشته‌اند با هیچی (empty string) جایگذاری می‌شوند::
218+
219+
>>> import re
220+
221+
>>> match = re.search('(\w+),(\w+),(\w+)', 'Jazz,Rock,Pop')
222+
>>> match.groups()
223+
('Jazz', 'Rock', 'Pop')
224+
225+
>>> match.expand('-->\1---->\2------>\3') # Warning!!!
226+
'-->\x01---->\x02------>\x03'
227+
228+
>>> match.expand('-->\\1---->\\2------>\\3')
229+
'-->Jazz---->Rock------>Pop'
230+
231+
>>> match.expand(r'-->\1---->\2------>\3')
232+
'-->Jazz---->Rock------>Pop'
233+
234+
235+
::
236+
237+
# \1, \2 and \3 are all valid escape characters
238+
239+
\1 # (U+0001 or 0x01) stands for the ascii start-of-heading character
240+
\2 # (U+0002 or 0x02) stands for the ascii start-of-text character
241+
\3 # (U+0003 or 0x03) stands for the ascii end-of-text character
242+
243+
.. note::
244+
در نمونه کد بالا، خروجی نخستین استفاده از متد ``expand`` متناسب با انتظار نیست، دلیل هم مربوط به وجود کاراکترهایی با ``\`` است (escape characters) که باعث بروز اخلال در تحلیل رشته قالب شده است. بهتر است همیشه در این مواقع از قوانین **raw string** پیروی نماییم: استفاده از ``\\`` به جای ``\`` (همانند ``n\\``) یا قرار دادن یک کاراکتر ``r`` یا ``R`` در ابتدای رشته (همانند ``'r'\n``). در این صورت کاراکترهایی همچون newline یا ``n\`` در رشته، معنای خود را از دست می‌دهند. (یادآوری از درس هفتم)
245+
246+
در واقع مفسر پایتون پیش از قراردادن متن مورد نظر ما در قالب یک شی رشته (string) در حافظه (memory) آن را تحلیل و مقادیر متناسب با کاراکترهای ``\`` را در آن جایگذاری می‌کند که این کار ممکن است در هنگام استفاده ماژول ``re`` از آن شی رشته اخلال ایجاد کند. استفاده از **raw string** باعث می‌شود مفسر پایتون متن مورد نظر را بدون تغییر در حافطه قرار دهد.
247+
248+
249+
.. tip::
250+
بلای Backslash [`اسناد پایتون <https://docs.python.org/3/howto/regex.html#the-backslash-plague>`__]
251+
252+
همیشه در هنگام کار با RegEx (نه فقط در زبان پایتون!) مواظب escape characters یا همان backslash characters باشید. تا این لحظه برای جلوگیری از پیچیدگی در مثال‌های ارائه شده مبحث RegEx از قرار دادن **raw string** صرف نظر شده بود اما از آنجا که الگوهای RegEx پر از ``\`` است همواره می‌بایست به لزوم استفاده از **raw string** فکر کنیم.
253+
254+
::
255+
256+
>>> match = re.search(r'(?P<num>\d+)', 'Top 100 songs')
257+
>>> match.group(1)
258+
'100'
259+
260+
>>> match.expand(r'--- \g<num> ---')
261+
'--- 100 ---'
262+
>>> match.expand(r'--- \g<1> ---')
263+
'--- 100 ---'
264+
265+
266+
267+
215268
* ``Match.start([group])`` [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.Match.start>`__] ``Match.end([group])`` [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.Match.end>`__]
216269

217270
متن رشته خروجی (تطبیق یافته بر اساس الگو مورد نظر) را در نظر بگیرید، متد ``start`` اندیس شروع این متن از رشته ورودی و متد ``end`` اندیس نقطه پایان را برمی‌گرداند. این دو متد می‌توانند یک آرگومان اختیاری نیز دریافت کنند که معرف اندیس یک گروه مشخص در الگو می‌باشد، با ارسال این آرگومان نتایج بر اساس تکه متن تطبیق داده شده با آن گروه برگردانده خواهد شد::
@@ -291,7 +344,120 @@
291344
'24.1632'
292345

293346

347+
تابع ``match``
348+
~~~~~~~~~~~~~~~~~~~~~~
349+
350+
351+
``match(pattern, string, flags=0)``
352+
353+
تابع ``match`` از ابتدای string انطباق pattern را انجام می‌دهد، در صورت موفقیت یک شی ``Match`` و در غیر این صورت ``None`` برمی‌گرداند [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.match>`__]::
354+
355+
>>> import re # Python 3.x
356+
357+
>>> match = re.match(r'\d+', '123@USERNAME')
358+
>>> print(match)
359+
<re.Match object; span=(0, 3), match='123'>
360+
361+
>>> match = re.match(r'\d+', 'USERNAME@123')
362+
>>> print(match)
363+
None
364+
365+
366+
>>> match = re.search(r'\d+', '123@USERNAME')
367+
>>> print(match)
368+
<re.Match object; span=(0, 3), match='123'>
369+
370+
>>> match = re.search(r'\d+', 'USERNAME@123')
371+
>>> print(match)
372+
<re.Match object; span=(9, 12), match='123'>
373+
374+
375+
.. tip::
376+
377+
تفاوت دو تابع ``match`` و ``search`` [`اسناد پایتون <https://docs.python.org/3/library/re.html#search-vs-match>`__]:
378+
379+
هنگام استفاده از تابع ``match``، از همان ابتدای متن مورد نظر می‌بایست با الگو تطابق صورت پذیرد (حتی در متن‌های چند سطری) ولی تابع ``search`` انجام انطباق را در هر جایی از متن دنبال می‌کند.
380+
381+
هنگام استفاده از نشانه ``re.MULTILINE`` در تابع ``search``، کاراکتر ``^`` در الگو از معنای **ابتدای متن** به معنای **ابتدای هر سطر** تغییر می‌کند ولی از نظر تابع ``match`` وجود کاراکتر ``^`` در الگو همواره به معنی ابتدای متن می‌باشد (نه هر سطر)::
382+
383+
>>> import re
384+
>>> string = 'Perl\nPython\nRuby' # 3 lines
385+
386+
>>> match = re.search('^Perl', string)
387+
>>> print(match)
388+
<re.Match object; span=(0, 4), match='Perl'>
389+
390+
>>> match = re.search('^Python', string)
391+
>>> print(match)
392+
None
393+
394+
>>> match = re.search('^Python', string, re.MULTILINE)
395+
>>> print(match)
396+
<re.Match object; span=(5, 11), match='Python'>
397+
398+
399+
>>> match = re.match('^Perl', string)
400+
>>> print(match)
401+
<re.Match object; span=(0, 4), match='Perl'>
402+
403+
>>> match = re.match('^Python', string, re.MULTILINE)
404+
>>> print(match)
405+
None
406+
407+
408+
اجازه بدهید یادآوری کنیم که دو نمونه کد زیر عملکردی معادل یکدیگر دارند::
409+
410+
411+
>>> pattern = re.compile('Py...n')
412+
>>> match = pattern.match('Python is great')
413+
414+
::
415+
416+
>>> match = re.match('Py...n', 'Python is great')
417+
418+
419+
تابع ``fullmatch``
420+
~~~~~~~~~~~~~~~~~~~~~~
421+
422+
423+
``fullmatch(pattern, string, flags=0)``
424+
425+
این تابع (``fullmatch``) چنانچه تمام string با pattern انطباق داشته باشد یک شی ``Match`` و در غیر این صورت ``None`` برمی‌گرداند [`اسناد پایتون <https://docs.python.org/3/library/re.html#re.fullmatch>`__] - این تابع از **پایتون نسخه 3.4** به بعد در دسترس است::
426+
427+
>>> import re # Python >= 3.4
428+
429+
>>> match = re.fullmatch(r'\d+', '123@USERNAME')
430+
>>> print(match)
431+
None
432+
433+
>>> match = re.fullmatch(r'\d+', '123')
434+
>>> print(match)
435+
<re.Match object; span=(0, 3), match='123'>
436+
437+
**عملکرد نمونه کدهای زیر برابر هم هستند - به الگو و نام توابع توجه نمایید**::
438+
439+
>>> match = re.search(r'^\d+$', '123')
440+
>>> print(match)
441+
<re.Match object; span=(0, 3), match='123'>
442+
443+
>>> match = re.match(r'\d+$', '123')
444+
>>> print(match)
445+
<re.Match object; span=(0, 3), match='123'>
446+
447+
>>> match = re.fullmatch(r'\d+', '123')
448+
>>> print(match)
449+
<re.Match object; span=(0, 3), match='123'>
450+
451+
452+
همچنین باید یادآوری کنیم که دو نمونه کد زیر عملکردی معادل یکدیگر دارند::
453+
454+
455+
>>> pattern = re.compile('Py...n')
456+
>>> match = pattern.fullmatch('Python')
457+
458+
::
294459

460+
>>> match = re.fullmatch('Py...n', 'Python')
295461

296462

297463

0 commit comments

Comments
 (0)