J3i9G

          Chompy story: Singularity was reached in late 197x. 
          However, Larry Wall made his perfect job on creating 
          Perl in mid 198x, saying it loud that AWK wasn't 
          the last thing happend on human Earth. Agree.

            ape.. 
            homo sapience.. 
            cyborg (organic body, shared internet based brain)..
            SS (silicon sapience)..

## HIGHLIGHTING

On most terminals (with the exception of Windows' cmd.exe, of course) it is possible to colorize output using the \033 ANSI escape sequence*.

*) https://stackoverflow.com/questions/4842424
     /list-of-ansi-color-escape-sequences
     
Select Graphic Rendition subset:

  \033[XXXm

where XXX is a series of semicolon-separated parameters.

  +---------+------------------------------------+ +---------------+
  |  Code   |               Effect               | |           BGR |
  +---------+------------------------------------+ +---------------+
  | 0       |  Reset all attributes              | | black     000 |
  | 1       |  Bold or Bright or non transparent | | red       001 |
  | 4       |  Underline                         | | green     010 |
  | 7       |  Invert                            | | yellow    011 |
  | 30-37   |  Set foreground color              | | blue      100 |
  | 40-47   |  Set background color              | | magenta   101 |
  | 90-97   |  Set bright foreground color       | | cyan      110 |
  | 100-107 |  Set bright background color       | | white     111 |
  +---------+------------------------------------+ +---------------+

Examples: 

1. Red underlined:

  echo -e "\033[31;1;4mHello\033[0m"
  
2. Estonian flag:

  echo -e "\033[104m\n      \033[40m\n      \033[7m\n      \033[0m\n"
                ----             -               -              -
                fg               bg              invert         reset
                
  m -- terminate sequence

3. Show what you have found:

  echo -e `echo babaa | perl -pe 's/(a+)/\\033\[31;1m\1\\033\[0m/g'`
  
4. Chompy:

  echo -e `cat mytext | perl -pe 's/\n/\\033\[101m<NEWLINE>\\033\[0m/'
  cat mytext
  
  one
  two
  three


J3HA0

          Лень, как обузданная жадность 
          (Laziness as Curb Greediness)

## REGULAR EXPRESSIONS

Initially, there were 12 metasymbols (Henry Spencer's 10 commandments, 12 apostoles, etc):

  . + * ? ^ $ ( [ ] { } |
  
Not having ')' as a metasymbol caused a little chaos. Problem was solved using the rule: "every symbol which is not a metasymbol have it's clone in extended alphabet (Passed through a wormhole '\')", so 

  $ echo "ppqqrr" | sed 's/q\q/GOTCHA/'
  ppGOTCHArr
  
Pair consists of two halves, one half plus one half still one, so it's ten now (Basic Regex):

  . + * ? ^ $ () [] {} |
  
However, some metasymbols were in basic alphabet, but some in extended. No good. Larry Wall reduced entropy by introducing PCRE (Perl Compatible Regular Expressions) -- now every meta symbol (of 10) is in main alphabet pushing out through a wormhole it's old meaning (just a symbol).

  $ echo '. + * ? ^ $ ( ) [ ] { } |' | \
    perl -pe 's/\. \+ \* \? \^ \$ \( \) \[ \] \{ \} \|/GOTCHA/'
  GOTCHA

Compare with basic REGEX (tested with Ubuntu 18.04 WSL):

  $ echo '. + * ? ^ $ ( ) [ ] { } |' | \
    sed 's/\. + \* ? \^ \$ ( ) \[ \] { } |/GOTCHA/' 
  GOTCHA
  
Evil wasn't slept too long introducing pretty big set of new metasymbols, and injecting them directly.. into extended alphabet space. The chaos was restored, nothing interferes.
 
 
# REGEX SYNTAX

Character: All characters, except those having special meaning in regex, matches themselves.

Special Regex Characters: ., +, *, ?, ^, $, (, ), [, ], {, }, |

Escape Sequences (\char): To match a character having special meaning in regex, you need to use an escape sequence prefix with a backslash (\). You also need to use \\ matches "\". Regex recognizes common escape sequences such as:

  \n for newline, 
  \t for tab, 
  \nnn for a up to 3-digit octal number, 
  \xhh for a two-digit hex code 

Example

  $ echo AAA | perl -pe 's/\x41/S/g'
  SSS 


Concatenate, Alterate, Replicate and Group 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CARGo -- existed at the very beginning of this story (1940)*.
------
*) Stephen Kleene's "Kleene star" -- It is a phrase you should know.
(https://chortle.ccsu.edu/FiniteAutomata/Section07/sect07_16.html)
------

1. CONCATENATE -- a Sequence of Characters: (Kleene). The matching, by default, is case-sensitive, but can be set to case-insensitive via modifier.

2. ALTERATE -- OR Operator '|'

Bracket List:

   a) [...]: ANY ONE of the character, e.g., [aeiou] matches "a", "e", "i", "o" or "u".
   
   b) [.-.] (Range Expression): ANY ONE of the character in the range, e.g., [0-9] matches any digit.
   
   c) [^...]: NOT ONE of the character, e.g., [^0-9] matches any non-digit.

Only these four characters require escaping inside the bracket list: ^, -, ], \.

3. REPLICATE (or Kleene closure):
   
  +: one or more (1+), e.g., [0-9]+ matches one or more digits such as '123', '000'.
   
  *: zero or more (0+), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
   
  ?: zero or one (optional), e.g., [+-]? matches an optional "+", "-", or an empty string.
  
  {m,n}: m to n (both inclusive)
  
  {m}: exactly m times
  
  {m,}: m or more (m+)

4. GROUP ( )

  $ echo 'The quick brown fox jumps over the lazy dog' | \
    perl -pe 's/(fox|dog)/\U\1/g'
  The quick brown FOX jumps over the lazy DOG
  

UNIX time (1970..now)
~~~~~~~~~~~~~~~~~~~~~

5. Metacharacters: matches a character
  
  . (dot): ANY ONE character except newline. Same as [^\n]
  
Position Anchors: does not match character, but position such as start-of-line or end-of-line.

  ^, $: start-of-line and end-of-line respectively. E.g., ^[0-9]$ matches a numeric string.
  
6. Laziness (Curb Greediness for Repetition Operators): *?, +?, ??, {m,n}?, {m,}?

7. Implanting imperative concept:

Thanks God, "label -- goto" pattern became outcast when this happend.

Parenthesized Back References:
   
   a) Use parentheses ( ) to create a back reference.
   b) Use $1, $2, ... (Java, Perl, JavaScript) or \1, \2, ... (Python, BASH) to retreive the back references in sequential order.
        

Things Tend To Grow
~~~~~~~~~~~~~~~~~~~

More metacharacters:

  \d, \D: ANY ONE digit/non-digit character. Digits are [0-9]
  
  \w, \W: ANY ONE word/non-word character. For ASCII, word characters are [a-zA-Z0-9_]
  
  \s, \S: ANY ONE space/non-space character. For ASCII, whitespace characters are [ \n\r\t\f]

More anchors:

  \b: boundary of word, i.e., start-of-word or end-of-word. E.g., \bcat\b matches the word "cat" in the input string.
  
  \B: Inverse of \b, i.e., non-start-of-word or non-end-of-word.
  
  \<, \>: start-of-word and end-of-word respectively, similar to \b. E.g., \<cat\> matches the word "cat" in the input string.


And then UTF-8 became dominant..

  $ echo Райские яблочки | tr и i
  i▒i▒i▒сi▒iii▒ яi▒i▒i▒чi▒ii
  
With Debian 9 Stretch:

  $ sudo apt install \
      flex bison ed mc zlib1g-dev libncurses-dev \
      make build-essential cvs
  $ cvs \
  -d:pserver:anonymous@heirloom.cvs.sourceforge.net:/cvsroot/heirloom \
  co -P heirloom
  $ cd heirloom/
  $ sudo ln -s /bin/sh /sbin/sh
  $ find nawk/ -name *.c -exec grep -Hn '<stdint.h>' {} +
  nawk/lib.c:32:#include <stdint.h>
  nawk/tran.c:33:#include <stdint.h>
  nawk/b.c:30:#include <stdint.h>
  nawk/proctab.c:2:#include <stdint.h>
  nawk/main.c:32:#include <stdint.h>
  nawk/parse.c:32:#include <stdint.h>
  nawk/maketab.c:32:#include <stdint.h>
  $ make
  $ cd tr
  $ echo Райские яблочки | tr и i
  i▒i▒i▒сi▒iii▒ яi▒i▒i▒чi▒ii
  $ echo Райские яблочки | ./tr и i
  Райскiе яблочкi


# LINKS

  1. https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

  2. http://www.grymoire.com/Unix/Regular.html        
