Zanurkuj w Pythonie/Rozwlekłe wyrażenia regularne: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
mNie podano opisu zmian
Linia 1:
{{Podświetl|py}}
== VerboseRozwlekłe Regularwyrażenia Expressionsregularne ==
 
Jak narazie mieliśmy do czynienia z czymś co nazywam "kompaktowymi" wyrażeniami regularnymi. Jak widziałeś, śa one trudne do odczytania i nawet jeśli już je rozszyfrujesz, nie ma gwarancji że zrobisz to za np. sześć miesięcy. To czego potrzebujemy to dokumentacja w ich treści.
So far you've just been dealing with what I'll call “compact” regular expressions. As you've seen, they are difficult to read, and even if you figure out what one does, that's no guarantee that you'll be able to understand it six months later. What you really need is inline documentation.
 
Python pozwala na to przez tworzenie tzw. rozwlekłych wyrażeń regularnych. Różnią się one od kompaktowych dwoma rzeczami:
Python allows you to do this with something called verbose regular expressions. A verbose regular expression is different from a compact regular expression in two ways:
* Białe znaki są ignorowane. Spacje, znaki tabulacji, znaki nowej linii nie są dopasowywane jako spacje, znaki tabulacji ani znaki nowej linii. Nie są wcale dopasowywane. (Jeśli byś chciał jednak dopasować któryś z nich musisz poprzedzić je backslashem.)
* Whitespace is ignored. Spaces, tabs, and carriage returns are not matched as spaces, tabs, and carriage returns. They're not matched at all. (If you want to match a space in a verbose regular expression, you'll need to escape it by putting a backslash in front of it.)
* Komentarze są ignorowane. Komentarz w rozwlekłym wyrażeniu regularnym wygląda dokładnie tak samo jak w kodzie Pythona: zaczyna się od # i leci aż do końca linii. W tym przypadku jest to komentarz w wieloliniowym łańcuchu znaków, ale zasada działania jest taka sama.
* Comments are ignored. A comment in a verbose regular expression is just like a comment in Python code: it starts with a # character and goes until the end of the line. In this case it's a comment within a multi-line string instead of within your source code, but it works the same way.
 
Łatwiej będzie to zrozumieć jeśli skorzystamy z przykładu. Weźmy kompaktowe wyrażenie regularne jakie tworzyliśmy wcześniej i zmieńmy je w rozwlekłe. Ten przykład pokazuje jak.
This will be more clear with an example. Let's revisit the compact regular expression you've been working with, and make it a verbose regular expression. This example shows how.
 
'''ExamplePrzykład 7.9. RegularWyrażenia Expressionsregularne withz Inline Commentskomentarzami'''
 
>>> pattern = """
^ # beginningpoczątek ofłańcucha stringznaków
M{0,4} # thousandstysiące - 0 todo 4 M's
(CM|CD|D?C{0,3}) # hundredssetki - 900 (CM), 400 (CD), 0-300 (0 todo 3 C's),
# orlub 500-800 (D, followeda bypo nim 0 todo 3 C's)
(XC|XL|L?X{0,3}) # tensdziesiątki - 90 (XC), 40 (XL), 0-30 (0 todo 3 X's),
# or 50-80 (L, followeda bypo nim 0 todo 3 X's)
(IX|IV|V?I{0,3}) # onesjedności - 9 (IX), 4 (IV), 0-3 (0 todo 3 I's),
# orlub 5-8 (V, followeda bypo nim 0 todo 3 I's)
$ # endkoniec ofłańcucha stringznaków
"""
>>> re.search(pattern, 'M', re.VERBOSE) #(1)
Linia 31:
>>> re.search(pattern, 'M') #(4)
 
# Najważniejszą rzeczą o której należy pamiętać przy korzystaniu z rozwlekłych wyrażeń regularnych jest to, że musisz przekazać dodatkowy argument: re.VERBOSE. Jest to stała zdefiniowana w module re która sygnalizuje, że wyrażenie powinno być traktowane jako rozwlekłe. Jak widzisz, ten wzorzec ma mnóstwo białych znaków (które są ignorowane) i kilka komentarzy (które też są ignorowane). Gdy zignorujesz i białe znaki i komentarze, to pozostanie dokładnie to samo wyrażenie regularne jakie otrzymaliśmy w poprzednim przykładzie, ale o wiele bardziej czytelne.
# The most important thing to remember when using verbose regular expressions is that you need to pass an extra argument when working with them: re.VERBOSE is a constant defined in the re module that signals that the pattern should be treated as a verbose regular expression. As you can see, this pattern has quite a bit of whitespace (all of which is ignored), and several comments (all of which are ignored). Once you ignore the whitespace and the comments, this is exactly the same regular expression as you saw in the previous section, but it's a lot more readable.
# To dopasowuje początek łańcucha, potem jedno z czterech możliwych M, potem CM, L i trzy z trzech możliwych X, potem IX i koniec łańcucha.
# This matches the start of the string, then one of a possible four M, then CM, then L and three of a possible three X, then IX, then the end of the string.
# To dopasowuje początek łańcucha, potem cztery z czterech możliwych M, dalej D, trzy z trzech możliwych C, L z trzema możliwymi X, potem V z trzema możliwymi I i na koniec koniec łańcucha.
# This matches the start of the string, then four of a possible four M, then D and three of a possible three C, then L and three of a possible three X, then V and three of a possible three I, then the end of the string.
# Tutaj nie udało się dopasować niczego. Czemu? Ponieważ nie przekazaliśmy flagi re.VERBOSE, więc funkcja re.search traktuje to wyrażenie regularne jako kompaktowe, z dużą ilością białych znaków i hash'ów. Python nie rozpoznaje samodzielnie czy każemy mu dopasować kompaktowe czy rozwlekłe wyrażenie regularne i przyjmuje, że każde jest kompaktowe, chyba że wyraźnie wskażemy że tak nie jest.
# This does not match. Why? Because it doesn't have the re.VERBOSE flag, so the re.search function is treating the pattern as a compact regular expression, with significant whitespace and literal hash marks. Python can't auto-detect whether a regular expression is verbose or not. Python assumes every regular expression is compact unless you explicitly state that it is verbose.
 
<noinclude>