Regex regular expressions

From Ever changing code
Revision as of 19:58, 14 May 2020 by Pio2pio (talk | contribs) (→‎PCRE Perl Compatible Regular Expressions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

General expressions

+ -unlimited string
\ -escape/protect character
/[abc]+/ -matches a bb ccc

grep and regular expressions

grep understands three different versions of regular expression syntax:

  • basic (BRE),
  • extended (ERE), -E, --extended-regexp
  • perl (PCRE), -P, --perl-regexp


In GNU grep there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl-compatible regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but work only if PCRE is available in the system. The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any meta-character with special meaning may be quoted by preceding it with a backslash. The period . matches any single character.


Basic vs Extended Regular Expressions

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).


There is a number of special meta characters that need to be escaped but not all:

# Characters in grep to escape
# \(\) - braces

# Chars that don't need escaping
# []
# |


Examples:

# Find 2+ consecutive digits, even if separated by a space
echo "1234 5678 9101 1234" | grep '\([0-9]\) *\1'  #    basic BRE, meta characters need escaping
echo "1234 5678 9101 1234" | grep -E '([0-9]) *\1' # extended ERE, brackets are not escaped
1234 5678 9101 1234
#            ^ ^ # <- matched pattern:


Word boundaries

The GNU extensions to POSIX regular expressions add support for the \b and \B word boundaries, as described above. GNU also uses its own syntax for start-of-word and end-of-word boundaries. \< matches at the start of a word, like Tcl's \m. \> matches at the end of a word, like Tcl's \M.

echo 'To eat this worlds due, by the grave and thee.' | grep '\bthe\b'
echo 'To eat this worlds due, by the grave and thee.' | grep '\<the\>'
To eat this worlds due, by the grave and thee.
                           ^^^

Regular Expression Matching (REMATCH)

Regular Expression Matching is performed when using command like below, if the expression matches the string, the matched part of the string is stored in the BASH_REMATCH array.

[[ string =~ regexp ]]
[[ "abcde" =~ b.d ]]; echo ${BASH_REMATCH[@]} # -> bcd
[[ "/home/peter/index.html" =~ ^(.*)/(.*)\.(.*)$ ]];
echo ${BASH_REMATCH[@]} # -> /home/peter/index.html /home/peter index html

# Now:
#   BASH_REMATCH[0]=/home/peter/index.html
#   BASH_REMATCH[1]=/home/peter
#   BASH_REMATCH[2]=index
#   BASH_REMATCH[3]=html

PCRE Perl Compatible Regular Expressions

This also covers Notepad ++

regex like [\r\n]+ matches CRLF in Notepad ++

Notetad++ | npp | npp++

Find, replace and remove matching line. Find any occurances of help</help>, ending with \r?\n. \r is optional in case the file doesn't have Windows line endings.

Find what: ^help\r?\n
Replace with:


Join 2 lines with colon
Find what: ^(.*)[\r\n]+
Replace with: '\1: ' #single quotes are not needed


Join 2 lines with colon
Before After
Extensions
None
Cloud init
No
TAGS
Name
bastion-1 (All resources to be created)
Extensions: None
Cloud init: No
TAGS: Name
bastion-1 (All resources to be created)
Resources

Forepoint aka legacy name Websense

It accepts reg expresions with limited form example:

  • (.) -same as * wildcard matches any length of string and brackets are Reg-ex delimiters

References