Difference between revisions of "Regex regular expressions"
Line 3: | Line 3: | ||
\ -escape/protect character | \ -escape/protect character | ||
/[abc]+/ -matches a bb ccc | /[abc]+/ -matches a bb ccc | ||
= [http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html <code>grep</code> and regular expressions] = | |||
<code>grep</code> understands three different versions of regular expression syntax: | |||
* basic (BRE), | |||
* extended (ERE), <code>-E, --extended-regexp</code> | |||
* perl (PCRE), <code>-P, --perl-regexp</code> | |||
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 <code>.</code> matches any single character. | |||
;Basic vs Extended Regular Expressions | |||
In basic regular expressions the meta-characters <code>?, +, {, |, (, and )</code> lose their special meaning; instead use the backslashed versions <code>\?, \+, \{, \|, \(, and \)</code>. | |||
There is a number of special meta characters that need to be escaped but not all: | |||
<source lang=bash> | |||
# Characters in grep to escape | |||
# \(\) - braces | |||
# Chars that don't need escaping | |||
# [] | |||
# | | |||
</source> | |||
Examples: | |||
<source lang=bash> | |||
# Find 2+ consecutive digits, even if separated by a space | |||
echo "1234 5678 9101 1234" | grep '\([0-9]\) *\1' | |||
</source> | |||
= PCRE Perl Compatible Regular Expressions = | = PCRE Perl Compatible Regular Expressions = |
Revision as of 17:01, 10 November 2019
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'
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
- regex-remove-lines-containing Stackoverflow
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
- regex101.com Dynamic RegEx builder