Skip to content

Latest commit

 

History

History
55 lines (49 loc) · 18.3 KB

Regex.md

File metadata and controls

55 lines (49 loc) · 18.3 KB

Regex

Symbol Definition
(.) Matches anything (except for a newline) (escape the dot by using a slash .)
(-) hyphen (-) inside a character class specifies a range of characters where the left and right operands are the respective lower and upper bounds of the range
(^) if you use a caret (^) as the first character inside a character class, it will match anything that is not in that range
\d Matches any digit [0 - 9]
\D Matches any character that is not a digit
\s Matches any whitespace character [ \r\n\t\f ]
\S Matches any non-white space character
\w Matches with any word character
Word characters include alphanumeric characters (a-z, A-Z and 0-9) and underscores (_)
\W Matches non-word character
Non-word characters include characters other than alphanumeric characters (a-z, A-Z and 0-9) and underscores (_)
^ Matches the position at the start of a string
$ Matches the position at the end of a string
[a-z] [abcdefghijklmnopqrstuvwxyz]
[A-Z] [ABCDEFGHIJKLMNOPQRSTUVWXYZ]
[0-9] [0123456789]
{x} The tool {x} will match exactly x repetitions of character/character class/groups
w{3}: Will match the character w 3 times
[xyz]{5}: Will match the string of length 5 consisting of characters {x,y,z}
\d{4}: Will match any digit 4 times
{x,y} Will match between x and y (both inclusive) repetitions of character/character class/group
w{3,5}: Will match the character w 3, 4, or 5 times
[xyz]{5,}: Will match the character x,y, or z 5 or more times
\d{4}: Will match any digit 1,2,3, or 4 times
* Will match zero or more repetitions of character/character class/group
w*: Will match the character w 0 or more times
[xyz]*: Will match the the characters x,y, or z 0 or more times
\d*: Will match any digit 0 or more times
+ Will match one or more repetitions of character/character class/group
w+: Will match the character w 1 or more times
[xyz]+: Will match the the characters x,y, or z 1 or more times
\d+: Will match any digit 1 or more times
\b Asserts position at a word boundary:
Before the first character in the string, if the first character is a word character
Between two characters in the string, where one is a word character and the other is not a word character
After the last character in the string, if the last character is a word character
() Parentheses around a regular expression can group that part of regex together. This allows us to apply different quantifiers to that group. These parenthesis also create a numbered capturing. It stores the part of string matched by the part of regex inside parentheses
(?:) Used to create a non-capturing group. It is useful if we do not need the group to capture its match
| Alternations, denoted by the | character, match a single item out of several possible items separated by the vertical bar. When used inside a character class, it will match characters; when used inside a group, it will match entire expressions (i.e., everything to the left or everything to the right of the vertical bar). We must use parentheses to limit the use of alternations.
(Bob|Kevin|Stuart) will match either Bob or Kevin or Stuart
([a-f]|[A-F]) will match any of the following characters: a,b,c,d,e,f,A,B,C,D,E, or F

Examples

test string S

Example Regex
Match the string cesar var Regex_Pattern = 'cesar';
Match only and exactly strings of form: abc, def, ghi, jkx, where each variable a,b,c,d,e,g,h,i,j,k,x can be any single character except the new line var Regex_Pattern = /^...\....\....\....$/;
Match the pattern xxXxxXxxxx
x = a digit character
X = a non-digit character
var Regex_Pattern = /\d\d\D\d\d\D\d\d\d\d/;
Match the pattern XXxXXxXX
x = whitespace characters
X = non-whitespace characters
var Regex_Pattern = /\S\S\s\S\S\s\S\S/;
Match the pattern xxxXxxxxxxxxxxXxxx
x = word character
X = non-word character
var Regex_Pattern = /\w\w\w\W\w\w\w\w\w\w\w\w\w\w\W\w\w\w/;
Match the pattern Xxxxx
x = word character
X = digit
S must start with a digit X and end with . symbol
S should be only 6 characters long
var Regex_Pattern = /^\d\w\w\w\w\.$/;
Length 6
1: 1,2,3
2: 1,2,0
3: x,s,0
4: 3,0,A,a
5: x,s,u
6: . or ,
var Regex_Pattern = /^[1-3][0-2][xs0][30aA][xsu][.,]$/;
Length 6
1: not a digit (0-9)
2: not a lowercase vowel
3: not b,c,D,F
4: not a whitespace character
5: not an uppercase vowel
6: not a . or ,
var Regex_Pattern = /^[^0-9][^aeiou][^bcDF][^\s][^AEIOU][^.,]$/;
length >= 5
1: lowercase Eng alphabetic char
2: positive digit (0 is not pos/neg)
3: not a lowercase Eng alphabetic char
4: not an uppercase Eng alphabetic char
5: uppercase Eng alphabetic char
var Regex_Pattern = /^[a-z][1-9][^a-z][^A-Z][A-Z]/;
Length = 45
first 40 characters shoould consist of letters (upper/lower) or even digits
the last 5 characters should consist of odd digits or whitespace characters
var Regex_Pattern = /^[A-Za-z02468]{40}[13579\s]{5}$/;
Begin with 1 or 2 digits
3 or more letters (upper/lower)
end with up to 3 . symbols (0-3)
var Regex_Pattern = /^[\d]{1,2}[A-Za-z]{3,}[.]{0,3}$/;
Begin with 2 or more digits
0 or more lowercase letters
end with 0 or more uppercase letters
var Regex_Pattern = /^\d{2,}[a-z]*[A-Z]*$/;
Begin with 1 or more digits
1 or more uppercase letters
1 or more lowercase letters
var Regex_Pattern = /^\d+[A-Z]+[a-z]+$/;
Only lowercase and uppercase letters
end in s
var Regex_Pattern = /^[A-Za-z]*s$/;
Begin with a vowel (aeiouAEIOU)
any length consisting of only letters
start and end with a word boundary
var Regex_Pattern = /\b[aeiouAEIOU][A-Za-z]*\b/;
Should have 3 or more consecutive repetitions of ok var Regex_Pattern = /(ok){3,}/;
Must start with Mr., Mrs., Ms., Dr. or Er
The rest of the string must contain only one or more English alphabetic letters (upper and lowercase)
var Regex_Pattern = /^(Mr|Mrs|Ms|Dr|Er)\.[a-zA-Z]+$/;
The problem description contains at least 2 words `(/\w(\s+

Resources

Regex 101