forked from miczar1/djfz-24_25
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
dcdcad4cf6 |
@ -1,17 +0,0 @@
|
||||
import sys
|
||||
|
||||
def solve(lines):
|
||||
return [l for l in lines if "Hamlet" in l]
|
||||
|
||||
if __name__ == "__main__":
|
||||
lines = []
|
||||
|
||||
fp = sys.argv[1]
|
||||
assert(fp == "shakespeare.in") # just a sanity check
|
||||
|
||||
print(fp)
|
||||
with open(fp, encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
sol = solve(lines)
|
||||
with open("./shakespeare.out", 'w', encoding="utf-8") as f:
|
||||
f.writelines(sol)
|
@ -1,15 +0,0 @@
|
||||
import sys
|
||||
|
||||
# return if the sentence contains word pies
|
||||
def solve(lines):
|
||||
return [l for l in lines if "pies" in l.lower().split(" ")]
|
||||
|
||||
if __name__ == "__main__":
|
||||
lines = []
|
||||
|
||||
fp = sys.argv[1]
|
||||
with open(fp, encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
sol = solve(lines)
|
||||
with open("./polish_wiki_excerpt.out", 'w', encoding="utf-8") as f:
|
||||
f.writelines(sol)
|
@ -1,35 +0,0 @@
|
||||
import sys
|
||||
|
||||
# '19xx' literally would be accepted as well but we won't tell anyone
|
||||
def solve(lines):
|
||||
def detect_date(s):
|
||||
date_f = "19XX r."
|
||||
date_l = len(date_f)
|
||||
|
||||
j = 0
|
||||
for i in range(len(s)):
|
||||
c = s[i]
|
||||
|
||||
if j == date_l:
|
||||
return True
|
||||
elif c == date_f[j] \
|
||||
or (j in (2, 3) and c.isdigit()):
|
||||
j += 1
|
||||
continue
|
||||
|
||||
j = 0
|
||||
|
||||
return False
|
||||
|
||||
return filter(detect_date, lines)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
lines = []
|
||||
|
||||
fp = sys.argv[1]
|
||||
with open(fp, encoding="utf-8") as f: # following symlinks might not work on Windows
|
||||
lines = f.readlines()
|
||||
sol = solve(lines)
|
||||
with open("./polish_wiki_excerpt.out", 'w', encoding="utf-8") as f:
|
||||
f.writelines(sol)
|
@ -1,30 +0,0 @@
|
||||
import sys
|
||||
|
||||
# detect numbers. separate numbers by spaces
|
||||
def solve(lines):
|
||||
res = []
|
||||
|
||||
j = 0
|
||||
for l in lines:
|
||||
numbers = []
|
||||
for i in range(len(l)):
|
||||
c = l[i]
|
||||
|
||||
if j > 0 and not c.isdigit():
|
||||
numbers.append(l[i-j:i])
|
||||
j = j+1 if (c.isdigit() and c.isascii()) else 0
|
||||
|
||||
if numbers:
|
||||
res.append(' '.join(numbers))
|
||||
return '\n'.join(res)
|
||||
|
||||
if __name__ == "__main__":
|
||||
lines = []
|
||||
|
||||
fp = sys.argv[1]
|
||||
with open(fp, encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
sol = solve(lines)
|
||||
with open("./polish_wiki_excerpt.out", 'w', encoding="utf-8") as f:
|
||||
f.writelines(sol)
|
||||
f.write('\n') # it expects for file to end with newline :)
|
5
TaskB01/description.txt
Normal file
5
TaskB01/description.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Write a program to find lines containing the word "Hamlet".
|
||||
Make use of regular expressions.
|
||||
|
||||
POINTS: 1
|
||||
DEADLINE: 08-12-2024 23:59:59
|
169442
TaskB01/shakespeare.in
Normal file
169442
TaskB01/shakespeare.in
Normal file
File diff suppressed because it is too large
Load Diff
2
TaskB01/simple.exp
Normal file
2
TaskB01/simple.exp
Normal file
@ -0,0 +1,2 @@
|
||||
Here comes Hamlet
|
||||
Hamlet Hamlet again
|
3
TaskB01/simple.in
Normal file
3
TaskB01/simple.in
Normal file
@ -0,0 +1,3 @@
|
||||
Here comes Hamlet
|
||||
ABC
|
||||
Hamlet Hamlet again
|
7
TaskB02/description.txt
Normal file
7
TaskB02/description.txt
Normal file
@ -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.
|
||||
Make use of regular expressions.
|
||||
|
||||
POINTS: 1
|
||||
DEADLINE: 08-12-2024 23:59:59
|
50000
TaskB02/polish_wiki_excerpt.in
Normal file
50000
TaskB02/polish_wiki_excerpt.in
Normal file
File diff suppressed because one or more lines are too long
3
TaskB02/simple.exp
Normal file
3
TaskB02/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
Pies ma Alę
|
||||
Kot i pies to zwierzęta
|
||||
pies
|
5
TaskB02/simple.in
Normal file
5
TaskB02/simple.in
Normal file
@ -0,0 +1,5 @@
|
||||
Pies ma Alę
|
||||
Ala ma psa
|
||||
tu nic nie ma
|
||||
Kot i pies to zwierzęta
|
||||
pies
|
6
TaskB03/description.txt
Normal file
6
TaskB03/description.txt
Normal file
@ -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.
|
||||
Make use of regular expressions.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 08-12-2024 23:59:59
|
1
TaskB03/polish_wiki_excerpt.in
Symbolic link
1
TaskB03/polish_wiki_excerpt.in
Symbolic link
@ -0,0 +1 @@
|
||||
../TaskA02/polish_wiki_excerpt.in
|
3
TaskB03/simple.exp
Normal file
3
TaskB03/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
Kiedyś był 1934 r.
|
||||
Kiedyś był 1934 r.fsdfsdfsdf
|
||||
1934 r. to jakaś data
|
5
TaskB03/simple.in
Normal file
5
TaskB03/simple.in
Normal file
@ -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
|
6
TaskB04/description.txt
Normal file
6
TaskB04/description.txt
Normal file
@ -0,0 +1,6 @@
|
||||
Write a program to find all maximum substrings of digits.
|
||||
Return only these substrings separated by spaces in their order.
|
||||
Make use of regular expressions.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 08-12-2024 23:59:59
|
1
TaskB04/polish_wiki_excerpt.in
Symbolic link
1
TaskB04/polish_wiki_excerpt.in
Symbolic link
@ -0,0 +1 @@
|
||||
../TaskA02/polish_wiki_excerpt.in
|
4
TaskB04/simple.exp
Normal file
4
TaskB04/simple.exp
Normal file
@ -0,0 +1,4 @@
|
||||
34234 34 5
|
||||
34535
|
||||
34
|
||||
1992 1999
|
5
TaskB04/simple.in
Normal file
5
TaskB04/simple.in
Normal file
@ -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
|
602
lab-02.ipynb
Normal file
602
lab-02.ipynb
Normal file
@ -0,0 +1,602 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Języki formalne i złożoność obliczeniowa\n",
|
||||
"## Część 1: Wprowadzenie do biblioteki re\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "f5376d249e31d5aa"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Wyrażenia regularne będziemy robić na podstawie języka python3. Dokumentacja: https://docs.python.org/3.11/library/re.html\n",
|
||||
"\n",
|
||||
"Użyteczna strona do testowania wyrażeń regularnych: https://regex101.com/\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ce091d4ec144df32"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Podstawowe funkcje"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "35428280eb1f704"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Match\n",
|
||||
"match - zwraca dopasowanie od początku stringa"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "9674be284007455d"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"wynik = re.match(r'Ala', 'Ala ma kota. Kot ma Alę.')\n",
|
||||
"print(wynik)\n",
|
||||
"print(f\"Dopasowano: '{wynik.group()}', początek: {wynik.start()}, koniec: {wynik.end()}\")\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "fa7637b60ae679b5"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Search\n",
|
||||
"search - zwraca pierwsze dopasowanie w napisie\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "626c591ab52a086"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# search\n",
|
||||
"wynik = re.search(r'ni', 'Może nie najtaniej, ale jako tako.')\n",
|
||||
"\n",
|
||||
"if wynik:\n",
|
||||
" print(f\"Dopasowano: '{wynik.group()}', początek: {wynik.start()}, koniec: {wynik.end()}\")\n",
|
||||
"else:\n",
|
||||
" print('Nie znaleziono szukanego ciągu znaków.')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "4f5d7077a85b7108"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tekst = \"Kto zakłóca ciszę nocną musi ponieść karę: 100 batów!\"\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile('ci')\n",
|
||||
"\n",
|
||||
"wynik = wzorzec.search(tekst)\n",
|
||||
"\n",
|
||||
"if wynik:\n",
|
||||
" print(f\"Dopasowano: '{wynik.group()}', początek: {wynik.start()}, koniec: {wynik.end()}\")\n",
|
||||
"else:\n",
|
||||
" print('Nie znaleziono szukanego ciągu znaków.')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "b9da286f8d6936fb"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## findall\n",
|
||||
"findall - zwraca listę wszystkich dopasowań\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "600b87dd6af48c04"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# findall\n",
|
||||
"wzorzec = re.compile('o')\n",
|
||||
"\n",
|
||||
"wynik = wzorzec.findall(tekst)\n",
|
||||
"\n",
|
||||
"if len(wynik) > 0:\n",
|
||||
" print(type(wynik))\n",
|
||||
" print(f'Dopasowano: {wynik}')\n",
|
||||
"else:\n",
|
||||
" print(f'{wynik} Nie znaleziono szukanego ciągu znaków.')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "37a93d9d61c6178d"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"wzorzec = re.compile('o')\n",
|
||||
"\n",
|
||||
"wynik = wzorzec.finditer(tekst)\n",
|
||||
"for w in wynik:\n",
|
||||
" print(type(w))\n",
|
||||
" print(f'Dopasowano: \"{w.group()}\", początek: {w.start()}, koniec: {w.end()}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "3232906632de2fe1"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"*Uwaga*: pobierając z iteratora elementy, usuwamy je, nie można zatem ponownie się do nich odwołać."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "62e8a0370ee58827"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Użycie przedrostka 'r' w wyrażeniach regularnych w Pythonie\n",
|
||||
"\n",
|
||||
"W Pythonie przedrostek `r` przed łańcuchem znaków oznacza \"surowy\" łańcuch znaków (ang. *raw string*). Dlaczego jest to przydatne w kontekście wyrażeń regularnych?\n",
|
||||
"\n",
|
||||
"Wyrażenia regularne często używają znaków specjalnych, takich jak `\\d`, `\\w`, `\\b` itp. W standardowych łańcuchach znaków w Pythonie, znaki te mają specjalne znaczenie. Na przykład, `\\t` oznacza tabulację, a `\\n` oznacza nową linię.\n",
|
||||
"\n",
|
||||
"Jeśli chcemy użyć takiego wyrażenia regularnego w Pythonie, musielibyśmy podwajać znaki ukośnika, aby uniknąć konfliktu z wbudowanymi sekwencjami ucieczki w łańcuchach znaków, np. `\\\\d`, `\\\\w`."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "6342ff85cd5fc35a"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Bez użycia surowego łańcucha znaków\n",
|
||||
"result1 = re.findall(\"\\\\d+\", \"123 abc 456\")\n",
|
||||
"print(result1)\n",
|
||||
"\n",
|
||||
"# Używając surowego łańcucha znaków\n",
|
||||
"result2 = re.findall(r\"\\d+\", \"123 abc 456\")\n",
|
||||
"print(result2)\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "b09fa3103ec18719"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## sub\n",
|
||||
"Kolejną użyteczną metodą jest sub(), która pozwala na zmianę wzorca na inny ciąg znaków:"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "e9e3f41e90bbd746"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"wzorzec = re.compile(r'ni')\n",
|
||||
"\n",
|
||||
"zmieniony = wzorzec.sub('Ni!', tekst)\n",
|
||||
"print(zmieniony)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "af633211d7faca67"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## split\n",
|
||||
"split - dzieli napis na podstawie wzorca"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ea9205aa03ed7476"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"wzorzec = re.compile(r' ')\n",
|
||||
"\n",
|
||||
"wynik = wzorzec.split(tekst)\n",
|
||||
"print(f'Uzyskano {len(wynik)} wyniki/ów.')\n",
|
||||
"for w in wynik:\n",
|
||||
" print(w)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "71dc3b254a93f31c"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Metaznaki\n",
|
||||
"[] - zbiór znaków\n",
|
||||
"\n",
|
||||
". - jakikolwiek znak\n",
|
||||
"\n",
|
||||
"^ - początek napisu\n",
|
||||
"\n",
|
||||
"$ - koniec napisu\n",
|
||||
"\n",
|
||||
"? - znak występuje lub nie występuje\n",
|
||||
"\n",
|
||||
"\\* - zero albo więcej pojawień się\n",
|
||||
"\n",
|
||||
"\\+ - jeden albo więcej pojawień się\n",
|
||||
"\n",
|
||||
"{} - dokładnie tyle pojawień się\n",
|
||||
"\n",
|
||||
"| - lub\n",
|
||||
"\n",
|
||||
"() - grupa\n",
|
||||
"\n",
|
||||
"\\ - znak ucieczki\n",
|
||||
"\n",
|
||||
"\\d digit\n",
|
||||
"\n",
|
||||
"\\D nie digit\n",
|
||||
"\n",
|
||||
"\\s whitespace\n",
|
||||
"\n",
|
||||
"\\S niewhitespace"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "4b16abb6848f09ae"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"wzorzec = re.compile(r'.')\n",
|
||||
"print(wzorzec.findall(tekst))"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ac6669cac4f52d27"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# jeden lub wiecej / zero lub wiecej\n",
|
||||
"\n",
|
||||
"tekst = \"BCAAABGTAABBBCCTTSAGG4324242\"\n",
|
||||
"print(f'Łańcuch: {tekst}')\n",
|
||||
"wzorzec = re.compile(r'X+')\n",
|
||||
"print(f'Jeden lub więcej X: {wzorzec.findall(tekst)}')\n",
|
||||
"wzorzec = re.compile(r'X*')\n",
|
||||
"print(f'Zero lub więcej X: {wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "634e4b66a5081f97"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# zero lub jeden\n",
|
||||
"\n",
|
||||
"print(f'Łańcuch: {tekst}')\n",
|
||||
"wzorzec = re.compile(r'.?')\n",
|
||||
"print(wzorzec.findall(tekst))\n",
|
||||
"wzorzec = re.compile(r'.?T')\n",
|
||||
"print(wzorzec.findall(tekst))"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "44195e00a81b23fb"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(f'Łańcuch: {tekst}')\n",
|
||||
"wzorzec = re.compile(r'A+')\n",
|
||||
"print(f'Dopasowanie zachłanne: {wzorzec.findall(tekst)}')\n",
|
||||
"wzorzec = re.compile(r'A+?')\n",
|
||||
"print(f'Dopasowanie leniwe: {wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ea861a33e942d42a"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(f'Łańcuch: {tekst}')\n",
|
||||
"\n",
|
||||
"# dokladnie 3 dopasowania\n",
|
||||
"wzorzec = re.compile(r'A{3}')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# pomiedzy 2 i 3 dopasowania\n",
|
||||
"wzorzec = re.compile(r'A{2,3}')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# 2 lub wiecej dopasowan\n",
|
||||
"wzorzec = re.compile(r'A{2,}')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# 3 lub mniej dopasowan\n",
|
||||
"wzorzec = re.compile(r'A{,3}')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "751272b81731af36"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# poczatek lub koniec lancucha\n",
|
||||
"\n",
|
||||
"tekst = \"Ale pięknie pachnie! Maciek, co gotujesz?\"\n",
|
||||
"\n",
|
||||
"# poczatek lancucha\n",
|
||||
"wzorzec = re.compile(r'^Ale')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# poczatek lancucha\n",
|
||||
"wzorzec = re.compile(r'^ale')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# koniec lancucha\n",
|
||||
"wzorzec = re.compile(r'Ale$')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"# koniec lancucha + znak ucieczki\n",
|
||||
"wzorzec = re.compile(r'gotujesz\\?$')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "97bcd3ff7cc3913"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# grupy znakow\n",
|
||||
"wzorzec = re.compile(r'[A-Z]')\n",
|
||||
"print(f'Duże litery: {wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile(r'[a-z]')\n",
|
||||
"print(f'Małe litery: {wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile(r'[A-z]')\n",
|
||||
"print(f'Małe i duże litery: {wzorzec.findall(tekst)}')\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile(r'[aeiou]')\n",
|
||||
"print(f'Samogłoski: {wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ee099e6e1e5e2f80"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# wzorzec(?=X) - dopasowanie, jeśli po nim występuje X\n",
|
||||
"\n",
|
||||
"tekst = \"ACABADAHSAIIIQIIINSAODIANSAAGAGAGGGGPAAG\"\n",
|
||||
"\n",
|
||||
"print(f'Łańcuch: {tekst}')\n",
|
||||
"wzorzec = re.compile(r'G+(?=A)')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "dd6351952639ef42"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tekst = \"Ale się zrobiła świąteczna atmosfera.\"\n",
|
||||
"wzorzec = re.compile(r'Ale|świąteczna|zrobiła')\n",
|
||||
"print(f'{wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c7f3e1b9d16c91db"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Znaki specjalne\n",
|
||||
"\n",
|
||||
"\\s\t- biały znak\n",
|
||||
"\n",
|
||||
"\\S\t- nie-biały znak\n",
|
||||
"\n",
|
||||
"\\d\t- cyfra\n",
|
||||
"\n",
|
||||
"\\D\t- nie-cyfra\n",
|
||||
"\n",
|
||||
"\\w\t- znaki alfanumeryczne (litery i cyfry) oraz\n",
|
||||
" \n",
|
||||
"\\W\t- znaki nie-alfanumeryczne i nie\n",
|
||||
" \n",
|
||||
"\\b\t- początek lub koniec ,,słowa’’\n",
|
||||
" \n",
|
||||
"\\B\t- nie początek lub koniec ,,słowa''\n",
|
||||
"\n",
|
||||
"[a-z]\t- małe litery\n",
|
||||
"\n",
|
||||
"[A-Z]\t- wielkie litery\n",
|
||||
"\n",
|
||||
"[0-9]\t- cyfry\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "c1a7435b7887f5c2"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"wzorzec = re.compile(r'\\w+')\n",
|
||||
"print(f'Wyrazy: {wzorzec.findall(tekst)}')\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "3b1b1f147233455d"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Część II: Zadania praktyczne\n",
|
||||
"\n",
|
||||
"### Zadanie 1: Wyszukiwanie numerów telefonu\n",
|
||||
"\n",
|
||||
"Napisz wyrażenie regularne, które znajdzie wszystkie numery telefonu w tekście. Zakładamy, że numer telefonu ma format `xxx-xxx-xxx` lub `xxx xxx xxx`.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "e0670d19927add2b"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tekst = \"\"\"\n",
|
||||
"Jan: 123-456-789\n",
|
||||
"Anna: 987 654 321\n",
|
||||
"Karol: 456-789-123\n",
|
||||
"Zbyszek: 53252525342252\n",
|
||||
"Tytus: aaaa666432\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile(r\"\")\n",
|
||||
"print(f'Numery: {wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "aecbbf61477429ac"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tekst = \"\"\"\n",
|
||||
"jan.kowalski@gmail.com\n",
|
||||
"anna.zielinska@amu.edu.pl\n",
|
||||
"karol.nowak@interia.pl\n",
|
||||
"hello world\n",
|
||||
"@test.pl\n",
|
||||
"x@x\n",
|
||||
"fff22@gmail.com\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"wzorzec = re.compile(r\"\")\n",
|
||||
"print(f'Adresy: {wzorzec.findall(tekst)}')"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "1dcf9585d7b78073"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue
Block a user