Skip to content

Instantly share code, notes, and snippets.

@miranda-zhang
Last active May 15, 2019 03:05
Show Gist options
  • Save miranda-zhang/127ff89ca8737cdb9dac404955f7f34c to your computer and use it in GitHub Desktop.
Save miranda-zhang/127ff89ca8737cdb9dac404955f7f34c to your computer and use it in GitHub Desktop.
Regular expression

Regular expression cheatsheet

Digits

^     : Start anchor
[0-9] : Character class to match one of the 10 digits
{1,6} : Range quantifier. Minimum 1 repetition and maximum 6.
$     : End anchor

\d can be used as an equivalent of [0-9], i.e. ^\d{1,6}$

Letters

a-zA-Z

Optional

?

Any

. any char

\. the actual dot character

.? = .{0,1} match any char zero or one times

·* = .{0,} match any char zero or more times

.+ = .{1,} match any char one or more times

Escape

/=\/ []=\[\]

Grep

-c, --count print only a count of matching lines per FILE

--color=auto highlight matching strings if (and only if) the output is written directly to the terminal and said terminal is capable of displaying colored output.

-C,--context show lines around https://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines

-e PATTERN, --regexp=PATTERN

-E, --extended-regexp

-n, --line-number print line number with output lines

-o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-r, --recursive like --directories=recurse

Example: hightlight suffix

AccessControl___-1450542916.html         
IP___1246962346.html

grep -Eor --color=auto "___[-]?[0-9]{10,10}"

https://ftp.gnu.org/old-gnu/Manuals/grep-2.4/html_node/grep_6.html

  • \w == [[:alnum:]] == [0-9A-Za-z]
  • [0-9A-Za-z] is dependent upon the ASCII character encoding, whereas [[:alnum:]] is portable.
  • [:alnum:] Any of [:digit:] or [:alpha:]
  • \W == [^[:alnum]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment