diff --git a/README.md b/README.md index 7785a24..88518fa 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,190 @@ B05 - jedno dla wszystkich C00 - zadanie wykonywane wspólnie na zajęciach C01-C03, C04-C06 - po jedno dla każdego + +## Zajęcia 3 13.11.2023 Wyrażenia regularne + +D01 - D04 - po jedno dla każdego + +Dokumentacja wyrażeń regularnych w python3: https://docs.python.org/3/library/re.html + +### Podstawowe funkcje + +search - zwraca pierwsze dopasowanie w napisie + +findall - zwraca listę wszystkich dopasowań (nienakładających się na siebie) + +match - zwraca dopasowanie od początku string + +To tylko podstawowe funkcje, z których będziemy korzystać. W dokumentacji opisane są wszystkie. + +### Obiekt match + +``` +import re +answer = re.search('na','banan') +print(answer) +print(type(answer)) +print(answer.start()) +print(answer.end()) +print(answer.group()) + +answer = re.search('na','kabanos') +print(answer) + +if answer: + print(answer.group()) +else: + pass +``` + +### Metaznaki + + +- [] - zbiór znaków +- . - jakikolwiek znak + +- ^ - początek napisu +- $ - koniec napisu + +- ? - znak występuje lub nie występuje +- \* - zero albo więcej pojawień się +- \+ - jeden albo więcej pojawień się +- {} - dokładnie tyle pojawień się + +- | - lub +- () - grupa +- \ -znak ucieczki + +- \d digit +- \D nie digit +- \s whitespace +- \S niewhitespace + + +### Flagi + +Można użyć specjalnych flag, np: +`re.search('ma', 'AlA Ma KoTa', re.IGNORECASE)`. + +### Przykłady (objaśnienia na laboratoriach) + +``` +import re + +text = 'Ala ma kota i hamak, oraz 150 bananów.' + +re.search('ma',text) +re.match('ma',text) +re.match('Ala ma',text) +re.findall('ma',text) + +re.findall('[mn]a',text) +re.findall('[0-9]',text) +re.findall('[0-9abc]',text) +re.findall('[a-z][a-z]ma[a-z]',text) +re.findall('[a-zA-Z][a-zA-Z]ma[a-zA-z0-9]',text) +re.findall('\d',text) + +re.search('[0-9][0-9][0-9]',text) +re.search('[\d][\d][\d]',text) + +re.search('\d{2}',text) +re.search('\d{3}',text) + +re.search('\d+',text) + +re.search('\d+ bananów',text) +re.search('\d* bananów','Ala ma dużo bananów') +re.search('\d* bananów',text) +re.search('ma \d? bananów','Ala ma 5 bananów') +re.search('ma ?\d? bananów','Ala ma bananów') +re.search('ma( \d)? bananów','Ala ma bananów') + +re.search('\d+ bananów','Ala ma 10 bananów albo 20 bananów') +re.search('\d+ bananów$','Ala ma 10 bananów albo 20 bananów') + +text = 'Ala ma kota i hamak, oraz 150 bananów.' + +re.search('\d+ bananów',text) + +re.search('\d+\sbananów',text) + +re.search('kota . hamak',text) + +re.search('kota . hamak','Ala ma kota z hamakiem') + +re.search('kota .* hamak','Ala ma kota lub hamak') + +re.search('\.',text) + +re.search('kota|psa','Ala ma kota lub hamak') + +re.findall('kota|psa','Ala ma kota lub psa') + +re.search('kota (i|lub) psa','Ala ma kota lub psa') + +re.search('mam (kota).*(kota|psa)','Ja mam kota. Ala ma psa.').group(0) + +re.search('mam (kota).*(kota|psa)','Ja mam kota. Ala ma psa.').group(1) + +re.search('mam (kota).*(kota|psa)','Ja mam kota. Ala ma psa.').group(2) +``` + +### Przykłady wyrażenia regularne 2 (objaśnienia na laboratoriach) + +#### ^ +``` +re.search('[0-9]+', '123-456-789') +re.search('[^0-9][0-9]+[^0-9]', '123-456-789') +``` + +#### cudzysłów +'' oraz "" - oznaczają to samo w pythonie + +' ala ma psa o imieniu "Burek"' + +" ala ma psa o imieniu 'Burek' " + +' ala ma psa o imieniu \'Burek\' ' + +" ala ma psa o imieniu \"Burek\" " + +#### multiline string + +#### raw string + +przy raw string znaki \ traktowane są jako zwykłe znaki \ + +chociaż nawet w raw string nadal są escapowane (ale wtedy \ pozostają również w stringu bez zmian) + +https://docs.python.org/3/reference/lexical_analysis.html + +dobra praktyka - wszędzie escapować + +``` +'\\' +print('\\') + +r'\\' +print(r'\\') + + +print("abcd") +print("ab\cd") +print(r"ab\cd") + +print("ab\nd") +print(r"ab\nd") + + +print("\"") +print(r"\"") + +print("\") +print(r"\") + +re.search('\\', r'a\bc') +re.search(r'\\', r'a\bc') +re.search('\\\\', r'a\bc') +``` \ No newline at end of file diff --git a/TaskD01/description.txt b/TaskD01/description.txt new file mode 100644 index 0000000..e780448 --- /dev/null +++ b/TaskD01/description.txt @@ -0,0 +1,5 @@ +Write a program to find lines containing the word "Hamlet". +Do use regular expressions. + +POINTS: 1 +DEADLINE: 2021-12-04 23:59:59 diff --git a/TaskD01/simple.exp b/TaskD01/simple.exp new file mode 100644 index 0000000..8f93eae --- /dev/null +++ b/TaskD01/simple.exp @@ -0,0 +1,2 @@ +Here comes Hamlet +Hamlet Hamlet again diff --git a/TaskD01/simple.in b/TaskD01/simple.in new file mode 100644 index 0000000..3d2b578 --- /dev/null +++ b/TaskD01/simple.in @@ -0,0 +1,3 @@ +Here comes Hamlet +ABC +Hamlet Hamlet again diff --git a/TaskD02/description.txt b/TaskD02/description.txt new file mode 100644 index 0000000..b0b2c50 --- /dev/null +++ b/TaskD02/description.txt @@ -0,0 +1,7 @@ +Write a program to find lines containing the word "pies" separated by spaces. +The word does not need to have space on the left if it is the line beginning or space on the right if it is line ending. +Return line no matter of word "pies" casing. +Do use regular expressions. + +POINTS: 1 +DEADLINE: 2021-12-04 23:59:59 diff --git a/TaskD02/simple.exp b/TaskD02/simple.exp new file mode 100644 index 0000000..7b3620b --- /dev/null +++ b/TaskD02/simple.exp @@ -0,0 +1,3 @@ +Pies ma Alę +Kot i pies to zwierzęta +pies diff --git a/TaskD02/simple.in b/TaskD02/simple.in new file mode 100644 index 0000000..99827b0 --- /dev/null +++ b/TaskD02/simple.in @@ -0,0 +1,5 @@ +Pies ma Alę +Ala ma psa +tu nic nie ma +Kot i pies to zwierzęta +pies diff --git a/TaskD03/description.txt b/TaskD03/description.txt new file mode 100644 index 0000000..19a05d5 --- /dev/null +++ b/TaskD03/description.txt @@ -0,0 +1,6 @@ +Write a program to find lines containing date from 1900 to 1999 in format '19XX r.' no matter what on the left or right of the expression. +Note that part ' r.' is obligatory. +Do use regular expressions. + +POINTS: 1 +DEADLINE: 2021-12-04 23:59:59 diff --git a/TaskD03/simple.exp b/TaskD03/simple.exp new file mode 100644 index 0000000..7134c32 --- /dev/null +++ b/TaskD03/simple.exp @@ -0,0 +1,3 @@ +Kiedyś był 1934 r. +Kiedyś był 1934 r.fsdfsdfsdf +1934 r. to jakaś data diff --git a/TaskD03/simple.in b/TaskD03/simple.in new file mode 100644 index 0000000..66d0858 --- /dev/null +++ b/TaskD03/simple.in @@ -0,0 +1,5 @@ +Kiedyś był 1934 r. +Kiedyś był 1934 r.fsdfsdfsdf +Kiedyś był 1935 rok +1934 r. to jakaś data +1934 to też jakaś data diff --git a/TaskD04/description.txt b/TaskD04/description.txt new file mode 100644 index 0000000..668a081 --- /dev/null +++ b/TaskD04/description.txt @@ -0,0 +1,6 @@ +Write a program to find all maximum substrings of digits. +Return only these substrings separated by spaces in their order. +Do use regular expressions. + +POINTS: 2 +DEADLINE: 2021-12-04 23:59:59 diff --git a/TaskD04/simple.exp b/TaskD04/simple.exp new file mode 100644 index 0000000..c661a47 --- /dev/null +++ b/TaskD04/simple.exp @@ -0,0 +1,4 @@ +34234 34 5 +34535 +34 +1992 1999 diff --git a/TaskD04/simple.in b/TaskD04/simple.in new file mode 100644 index 0000000..25d660c --- /dev/null +++ b/TaskD04/simple.in @@ -0,0 +1,5 @@ +34234 34 dfd gfd 5 +34535 +fsdflskfjsdflk +fsdkfj sdf34fdfd +Firma powstała w 1992 r., z połączenia Authorware, Inc. (twórców pakietu Authorware) i MacroMind-Paracomp (producenta Macromind Director). W 1999 r. Macromedia zakupiła firmę Allaire i jej bi