Week 06

Regular Expressions

Záznam z prednášky

Oznamy

Motivácia

Regulárne výrazy

Základy regulárnych výrazov

+——————-+———————–+——————-+ | source | pattern | matches | +===================+=======================+===================+ | | | | | "Hello, world" | "Hello" | "Hello" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world" | "HELLO" | "" | | | | | +——————-+———————–+——————-+

+——————-+———————–+——————-+ | source | pattern | matches | +===================+=======================+===================+ | | | | | "Hello, world" | "Hello, " | "Hello, " | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world" | "Hello, " | "" | | | | | +——————-+———————–+——————-+

Metaznaky

+——————-+———————–+——————-+ | source | pattern | matches | +===================+=======================+===================+ | | | | | "Hello, world" | "^Hello" | "Hello" | | | | | +——————-+———————–+——————-+ | | | | | " Hello, world" | "^Hello" | "" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world" | "^world" | "" | | | | | +——————-+———————–+——————-+

+——————-+———————–+——————-+ | source | pattern | matches | +===================+=======================+===================+ | | | | | "Hello, world" | "Hello$" | "" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world" | "world$" | "world" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world!" | "world$" | "" | | | | | +——————-+———————–+——————-+

+——————-+———————–+——————-+ | source | pattern | matches | +===================+=======================+===================+ | | | | | "Hello, world" | "." | "H" | | | | "e" | | | | "l" | | | | "l" | | | | "o" | | | | "," | | | | " " | | | | "w" | | | | "o" | | | | "r" | | | | "l" | | | | "d" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world" | "^." | "H" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world!" | "..$" | "d!" | | | | | +——————-+———————–+——————-+ | | | | | "Hello, world!" | ".l." | "ell" | | | | "rld" | | | | | +——————-+———————–+——————-+

Escaping

Triedy znakov

Character Repetition

+———————–+———————–+———————–+ | source | pattern | matches | +=======================+=======================+=======================+ | | | | | "Regular expressions" | ".{5}" | "Regul" | | | | "ar ex" | | | | "press" | | | | | +———————–+———————–+———————–+ | | | | | "Regular expressions" | "[esr]{1, 3}" | "e" | | | | "r" | | | | "e" | | | | "res" | | | | "s" | | | | "s" | | | | | +———————–+———————–+———————–+ | | | | | "Regular expressions" | "[a-z]{3, }" | "egular" | | | | "expressions" | | | | | +———————–+———————–+———————–+

Kvantifikátory v regulárnych výrazoch

+———————–+———————–+———————–+ | source | pattern | matches | +=======================+=======================+=======================+ | | | | | "aabc abc bc" | "a*b" | "aab" | | | | "ab" | | | | "b" | | | | | +———————–+———————–+———————–+

* **plus** `'+'` - hovorí o tom, že to, čo je pred ním, sa musí nachádzať minimálne raz (zhora však nie je ohraničený počet výskytov)

+———————–+———————–+———————–+ | source | pattern | matches | +=======================+=======================+=======================+ | | | | | "aabc abc bc" | "a+b" | "aab" | | | | "ab" | | | | | +———————–+———————–+———————–+

* **question mark** `'?'` - hovorí o tom, že to, čo je pred ním, sa môže nachádzať max. raz alebo ani raz.

+———————–+———————–+———————–+ | source | pattern | matches | +=======================+=======================+=======================+ | | | | | "aabc abc bc" | "a?b" | "ab" | | | | "ab" | | | | "b" | | | | | +———————–+———————–+———————–+

Zoskupovanie vzorov

Matching Alternatives

+—————————+———————–+———————–+ | source | pattern | matches | +===========================+=======================+=======================+ | | | | | "Monday Tuesday Friday" | "(on|ues|rida)" | "on" | | | | "ues" | | | | "rida" | | | | | +—————————+———————–+———————–+ | | | | | "Monday Tuesday Friday" | "(Mon|Tues|Fri)day" | "Monday" | | | | "Tuesday" | | | | "Friday" | | | | | +—————————+———————–+———————–+ | | | | | "Monday Tuesday Friday" | "..(nd|esd|id)ay" | "Monday" | | | | "Tuesday" | | | | "Friday" | | | | | +—————————+———————–+———————–+

Regulárne výrazy v jazyku C

Compilation of Regular Expression

Matching

Grouping

Additional Resources