Differences between revisions 1 and 2
Revision 1 as of 2020-03-20 17:07:26
Size: 2331
Comment:
Revision 2 as of 2020-03-20 17:11:14
Size: 2337
Comment:
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
Why do we need regular expressions? To filter from the strings such that match the query. We are not looking for the entire string exactly , but for the string that has the desired substring. Regular expressions do not allow describing arbitrary strings. Its difference is that the class and characters in it are separated. It can describe almost any possible pattern, not bound to context and having no internal parts dependence (e. g. «a if precedes by b» or «integer number, than that number of characters» can not be described). Why do we need regular expressions?

To filter from the strings such that match the query.
We are not looking for the entire string exactly , but for the string that has the desired substring. Regular expressions do not allow describing arbitrary strings. Its difference is that the class and characters in it are separated. It can describe almost any possible pattern, not bound to context and having no internal parts dependence (e. g. «a if precedes by b» or «integer number, than that number of characters» can not be described).

15.1 (conspect) Regexp

Patterns

There are two types ways to work with them:

  1. Matching is testing if the whole string can be described by pattern.
  2. Searching is finding a substring that matches pattern.

An integer cannot be used for Patterns. So you can't request to show files consisting only of any number, you should classify it (for example its length=3).

Regular expressions

Why do we need regular expressions?

To filter from the strings such that match the query. We are not looking for the entire string exactly , but for the string that has the desired substring. Regular expressions do not allow describing arbitrary strings. Its difference is that the class and characters in it are separated. It can describe almost any possible pattern, not bound to context and having no internal parts dependence (e. g. «a if precedes by b» or «integer number, than that number of characters» can not be described).

grep-search for a substring in a text file.

grep August ycall / / - August Call in ycall.

grep '[7-9] ' ycal / / - highlighting digits from 7 to 9 in white

grep 'u. u' ycall / / - looking for where there is a substring from [u, < something>, u]. the return of August

grep command 'e*o' g ycall / / = > 'e, p*, o'. Here we look for , e e, r * (zero or more times), o

Line start marker: grep '^[0-9][0-9]*' ycall

End of line marker: grep '[0-9][0-9]*$' ycall

Search and replace

sed — stream editor.

  • replace once
       1 $ cal | sed 's/[12][23]/@@/' 
       2      March 2020     
       3 Su Mo Tu We Th Fr Sa
       4  1  2  3  4  5  6  7
       5  8  9 10 11 @@ 13 14
       6 15 16 17 18 19 20 21
       7 @@ 23 24 25 26 27 28
       8 29 30 31            
       9 
    
  • replace all (globally)

       1 $ cal | sed 's/[12][23]/@@/g'
       2      March 2020     
       3 Su Mo Tu We Th Fr Sa
       4  1  2  3  4  5  6  7
       5  8  9 10 11 @@ @@ 14
       6 15 16 17 18 19 20 21
       7 @@ @@ 24 25 26 27 28
       8 29 30 31
       9 
    

Extended regexp and dialects

Disadvantages of traditional regexp: it's not easy to:

  • Search for one regexp or another.

  • Use «one-or-more» repeater (ok, it's easy, but boring); also, "*" repeater is dangerous.

  • Use character class like letters or spaces (also boring).

  • Use those "\"-s every time (most boring, in fact).

HSE/ProgrammingOS/15_Regexp/Cospect_en (last edited 2020-03-20 17:11:14 by VasilyKireenko)