Regular Expression Cheats

https://regex101.com/  <— is a online regex builder ! you can copy your output and then build your expression

Medium.com has done an excellent/exceptional work and i did copy the content here for me for fast search, but all credit goes to him, dont hesitate to visit his page for some excellent work and information.
Referencehttps://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285

Basic topics 

^The matches any string that starts with The -> Try it!end$ matches a string that ends with end^The end$ exact string match (starts and ends with The end)roar matches any string that has the text roar in it

abc* matches a string that has ab followed by zero or more c -> Try it!abc+ matches a string that has ab followed by one or more cabc? matches a string that has ab followed by zero or one cabc{2} matches a string that has ab followed by 2 cabc{2,} matches a string that has ab followed by 2 or more cabc{2,5} matches a string that has ab followed by 2 up to 5 ca(bc)* matches a string that has a followed by zero or more copies of the sequence bca(bc){2,5} matches a string that has a followed by 2 up to 5 copies of the sequence bc

a(b|c) matches a string that has a followed by b or c (and captures b or c) -> Try it!a[bc] same as previous, but without capturing b or c

\d matches a single character that is a digit -> Try it!\w matches a word character (alphanumeric character plus underscore) -> Try it!\s matches a whitespace character (includes tabs and line breaks). matches any character -> Try it!

\D matches a single non-digit character -> Try it!

\$\d matches a string that has a $ before one digit -> Try it!


Intermediate topics

a(bc) parentheses create a capturing group with value bc -> Try it!a(?:bc)* 
using ?: we disable the capturing group -> Try it!a(?<foo>bc) using ?<foo> we put a name to the group -> Try it!
[abc] matches a string that has either an a or a b or a c -> is the same as a|b|c -> Try it![a-c] 
same as previous[a-fA-F0-9] a string that represents a single hexadecimal digit, case insensitively -> Try it![0-9]%
a string that has a character from 0 to 9 before a % sign[^a-zA-Z]        
a string that has not a letter from a to z or from A to Z. 
In this case the ^ is used as negation of the expression -> Try it!
<.+?>            matches any character one or more times included inside < and >, expanding as needed -> Try it!
<[^<>]+>         matches any character except < or > one or more times included inside < and > -> Try it!

Advanced topics

\babc\b performs a “whole words only” search -> Try it!

\Babc\B matches only if the pattern is fully surrounded by word characters -> Try it!

([abc])\1 using \1 it matches the same text that was matched by the first capturing group -> Try it!([abc])([de])\2\1 we can use \2 (\3, \4, etc.) to identify the same text that was matched by the second (third, fourth, etc.) capturing group -> Try it!(?<foo>[abc])\k<foo> we put the name foo to the group and we reference it later (\k<foo>). The result is the same of the first regex -> Try it!

d(?=r) matches a d only if is followed by r, but r will not be part of the overall regex match -> Try it!(?<=r)d matches a d only if is preceded by an r, but r will not be part of the overall regex match -> Try it!

d(?!r) matches a d only if is not followed by r, but r will not be part of the overall regex match -> Try it!(?<!r)d matches a d only if is not preceded by an r, but r will not be part of the overall regex match -> Try it!

Reference : https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285

More example for Medium.com !

Trim spaces — try it!

Matches text avoiding additional spaces

^[\s]*(.*?)[\s]*$

HTML Tag — try it!

Matches any valid HTML tag and the corresponding closing tag

<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)

Hexadecimal value — try it!

Matches any valid hex color inside text

\B#(?:[a-fA-F0–9]{6}|[a-fA-F0–9]{3})\b

Valid email (RFC5322) — try it!

Matches any valid email inside text

\b[\w.!#$%&’*+\/=?^`{|}~-]+@[\w-]+(?:\.[\w-]+)*\b

Username (simple) — try it!

Minimum length of 3, maximum length of 16, composed by letters, numbers or dashes.

/^[a-z0-9_-]{3,16}$/

Strong password — try it!

Minimum length of 6, at least one uppercase letter, at least one lowercase letter, at least one number, at least one special character

Image for post

(?=^.{6,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!"$%&\/\(\)\?\^\'\\\+\-\*]))^.*

2 of a kind— try it!

At least 2 letters (uppercase or lowercase) at any index, minimum length of 8, maximum length of 32

Image for post

Image for post

^(?=([0-9]*[a-z]){2,})([a-zA-Z0-9]{8,32})$

If you want to use capturing groups to get scheme, path, etc. (or add user-info, host, port…) feel free to ask it in comments!

^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$

IPv4 address — try it!

Matches any valid IPv4 address inside text

\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b

“Defanged” URL or IPv4 address — try it!

^(((h..ps?|f.p):\/\/)?(?:([\w\-\.])+(\[?\.\]?)([\w]){2,4}|(?:(?:25[0–5]|2[0–4]\d|[01]?\d\d?)\[?\.\]?){3}(?:25[0–5]|2[0–4]\d|[01]?\d\d?)))*([\w\/+=%&_\.~?\-]*)$

SSN — Social Security Number (simple) — try it!

If you want to check the validity of an SSN feel free to ask in comments!

^((?<area>[\d]{3})[-][\d]{2}[-][\d]{4})$

Alpha-numeric, literals, digits, lowercase, uppercase chars only

\w                //alpha-numeric only
[a-zA-Z]          //literals only
\d                //digits only
[a-z]             //lowercase literal only
[A-Z]             //uppercase literal only

 

Leave a Comment