Skip to content

Instantly share code, notes, and snippets.

@oilsinwater
Last active June 1, 2022 02:05
Show Gist options
  • Save oilsinwater/77f924f95bccfeb88e07d0a4355753de to your computer and use it in GitHub Desktop.
Save oilsinwater/77f924f95bccfeb88e07d0a4355753de to your computer and use it in GitHub Desktop.
RegEx notes

RegEx Notes

Regular Expression Basics

expression description
. Any character except newline
a The character a
ab The string ab
a b
a* 0 or more a's
\ Escapes a special character

##RegEx escape sequences "­Esc­api­ng" is a way of treating characters which have a special meaning in regular expres­sions literally, rather than as special charac­ters.

expression description
\ escape the following character
\Q begin literal sequence
\E end literal sequence

RegEx quantifiers

expression description
* 0 or more
+ 1 or more
? 0 or 1
{2} Exactly 2
{2, 5} Between 2 and 5
2, 2 or more

Note: default is greedy. Append ? for reluctant

RegEx groups & ranges

expression description
(...) capturing group
(?:...) passive non-capturing group
\Y Match the Y'th captured group
. Any character except new line (\n)
(a b)
[abc] Range (a or b or c)
[^abc] Not (a or b or c)
[a-q] Lower case letter from a to q
[A-Q] Upper case letter from A to Q
[0-7] Digit from 0 to 7
\x Group/­sub­pattern number "­x"

Note: Ranges are inclusive

RegEx character classes

expression description
[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
\O octal digit
\s One whitespace
\S One non-white-space
\w One word character
\W One non-word character
\x hexadecimal digit

##RegEx Anchors

expression description
^ start of string
$ end of string
\b word boundary
\B non-word boundary
< start of word
> end of word

##RegEx Flags (pattern modifiers)

expression description
g global match
i ignore case
i* case-sensitive
m ^ and $ match start and end of line
m* multiple lines
s* treat string as single line
x* allow comments and whitespace pattern
e* evaluate placement
U* ungreedy pattern

##RegEx special characters

expression description
\n Newline
\r Carriage return
\t Tab
\0 null character
\YYY Octal character YYY
\xYY Hexadecimal character YY
\uYYYY Hexadecimal character YYYY
\cY control character

##RegEx replacement

expression description
$$ inserts $
$& insert entire match
$` insert preceding string
$' insert the following string
$Y insert Y'th captured group
$n nth non-pa­ssive group
$2 "­xyz­" in /^(abc­(xy­z))$/
$1 "­xyz­" in /^(?:a­bc)­(xyz)$/
$` Before matched string
$' After matched string
$+ Last matched string
$& Entire matched string

Note: Some regex implem­ent­ations use \ instead of $.

##RegEx Assertions

expression description
?= lookahead assertion
?! negative lookahead
?<= lookbehind assertion
?!= or ?<! negative lookbehind
?> Once-only subexpression
?() condition, [if then]
?() (add pipe to end) condition, [if then else]*
?# comment

*: should be ?()| but wont print to table in markdown

@Sravan0124
Copy link

How to replace a all underscored string into spaces

@Sravan0124
Copy link

Example: hii_this_is_sudheer
output:hii this is sudheer
#please help me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment