#Programming_paradigms : Declarative versus Imperative
#Declarative
All about relations.
Does not state the order in which operations execute.
Focuses on what the program should accomplish without specifying how the program should achieve the result.
Expresses the logic of a computation without describing its control flow.
Common declarative languages include those of database query languages (e.g. #SQL, #XQuery), regular expressions (#Regex), logic programming, functional programming, and configuration management systems.
e.g. #Haskell, #Kanren (a dialect of #Scheme), #Prolog, #Wolfram_Language
#Imperative
Uses statements that change a program's state.
1. they state the order in which operations occur, with constructs that explicitly control that order
Allows side effects, in which state can be modified within one unit of code, and then read inside a different unit of code.
Imperative programming focuses on describing how a program operates.
an imperative program consists of commands for the computer to perform.
Many imperative programming languages (such as #Fortran, #BASIC, and #C) are abstractions of assembly language.
#Declarative
All about relations.
Does not state the order in which operations execute.
Focuses on what the program should accomplish without specifying how the program should achieve the result.
Expresses the logic of a computation without describing its control flow.
Common declarative languages include those of database query languages (e.g. #SQL, #XQuery), regular expressions (#Regex), logic programming, functional programming, and configuration management systems.
e.g. #Haskell, #Kanren (a dialect of #Scheme), #Prolog, #Wolfram_Language
#Imperative
Uses statements that change a program's state.
1. they state the order in which operations occur, with constructs that explicitly control that order
Allows side effects, in which state can be modified within one unit of code, and then read inside a different unit of code.
Imperative programming focuses on describing how a program operates.
an imperative program consists of commands for the computer to perform.
Many imperative programming languages (such as #Fortran, #BASIC, and #C) are abstractions of assembly language.
To avoid any confusion while working with #regex, we would use raw strings as r"expression".
Raw strings don't escape anything, which makes use of regular expressions easier.
Raw strings don't escape anything, which makes use of regular expressions easier.
Pattern matching
- is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match has to either be or not be an exact match. The patterns have the form of either sequences or trees.
Sequence patterns, like strings, are often described using regular expressions and matched using techniques such as backtracking.
#regex
- is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match has to either be or not be an exact match. The patterns have the form of either sequences or trees.
Sequence patterns, like strings, are often described using regular expressions and matched using techniques such as backtracking.
#regex
#regex
Regex Cheatsheet
All the rules apply to all of the three languages: #Python, #Perl (PCRE) and #JavaScript, unless stated otherwise.
Source: debuggex.com
Basics
Quantifiers
⚠️ Default is greedy. Append ? for reluctant.
Groups
Python and PCRE:
PCRE only:
Character Classes
Assertions
Python and PCRE:
PCRE:
Flags
Python and PCRE:
JavaScript only:
Special Characters
JavaScript and PCRE:
Hexadecimal character YY (
Replacement
Python only:
JavaScript only:
Escapes
PCRE only:
POSIX Classes
PCRE only:
Regex Cheatsheet
All the rules apply to all of the three languages: #Python, #Perl (PCRE) and #JavaScript, unless stated otherwise.
Source: debuggex.com
Basics
.: Any character except newlinea: The character aab: The string aba|b: a or ba*: 0 or more a's\: Escapes a special characterQuantifiers
*: 0 or more+: 1 or more?: 0 or 1{2}: Exactly 2{2, 5}: Between 2 and 5{2,}: 2 or more(,5}: Up to 5 (Python only)⚠️ Default is greedy. Append ? for reluctant.
Groups
(...): Capturing group(?:...): Non-capturing group\Y: Match the Y'th captured groupPython and PCRE:
(?P<Y>...): Capturing group named Y(?P=Y): Match the named group Y(?#...): CommentPCRE only:
(?>...): Atomic group(?|...): Duplicate group numbers(?R): Recurse into entire pattern(?Y): Recurse into numbered group Y(?&Y): Recurse into named group Y\g{Y}: Match the named or numbered group Y\g<Y>: Recurse into named or numbered group YCharacter Classes
[ab-d]: One character of: a, b, c, d[^ab-d]: One character except: a, b, c, d[\b]: Backspace character\d: One digit\D: One non-digit\s: One whitespace\S: One non-whitespace\w: One word character\W: One non-word characterAssertions
^: Start of string$: End of string\b: Word boundary\B: Non-word boundary(?=...): Positive lookahead(?!...): Negative lookaheadPython and PCRE:
\A: Start of string, ignores m flag\Z: End of string, ignores m flag(?<=...): Positive lookbehind(?<!...): Negative lookbehind(?()|): ConditionalPCRE:
\G: Start of matchFlags
i: Ignore casem: ^ and $ match start and end of linePython and PCRE:
s: . matches newline as wellx: Allow spaces and commentsL: Locale character classesu: Unicode character classes(?iLmsux): Set flags within regexJavaScript only:
g: Global MatchSpecial Characters
\n: Newline\r: Carriage return\t: Tab\YYY: Octal character YYY\xYY: Hexadecimal character YYJavaScript and PCRE:
\0: Null character\cY: Control character YHexadecimal character YY (
\uYY for JavaScript and \x{YY} for PCRE)Replacement
Python only:
\g<0>: Insert entire match\g<Y>: Insert match Y (name or number)\Y: Insert group numbered YJavaScript only:
$$: Inserts $$&: Insert entire match$`: Insert preceding string$': Insert following string$Y: Insert Y'th captured groupEscapes
PCRE only:
\Q..\E: Remove special meaningPOSIX Classes
PCRE only:
[:alnum:]: Letters and digits[:alpha:]: Letters[:ascii:]: Ascii codes 0 - 127[:blank:]: Space or tab only[:cntrl:]: Control characters[:digit:]: Decimal digits[:graph:]: Visible characters, except space[:lower:]: Lowercase letters[:print:]: Visible characters[:punct:]: Visible punctuation characters[:space:]: Whitespace[:upper:]: Uppercase letters[:word:]: Word characters[:xdigit:]: Hexadecimal digits#languageTheory #regex
Proof that PCRE is more powerful than mere regex
Context-free:
a^n b^n:
Context-sensitive:
a^n b^n c^n:
Proved by Semicolon
Proof that PCRE is more powerful than mere regex
Context-free:
a^n b^n:
(a(?1)?b)Context-sensitive:
a^n b^n c^n:
(?:a(?=a*(\1?+b)b*(\2?+c)))+\1\2Proved by Semicolon