>>>pet=re.compile(r'dog')>>>type(pet)<class'_sre.SRE_Pattern'>>>>bool(pet.search('They bought a dog'))True>>>bool(pet.search('A cat crossed their path'))False
Functions
Function
Description
re.findall
Returns a list containing all matches
re.finditer
Return an iterable of match objects (one for each match)
re.search
Returns a Match object if there is a match anywhere in the string
re.split
Returns a list where the string has been split at each match
re.sub
Replaces one or many matches with a string
re.compile
Compile a regular expression pattern for later use
re.escape
Return string with all non-alphanumerics backslashed
Flags
-
-
-
re.I
re.IGNORECASE
Ignore case
re.M
re.MULTILINE
Multiline
re.L
re.LOCALE
Make \w,\b,\slocale dependent
re.S
re.DOTALL
Dot matches all (including newline)
re.U
re.UNICODE
Make \w,\b,\d,\sunicode dependent
re.X
re.VERBOSE
Readable style
Regex in JavaScript
test()
lettextA="I like APPles very much";lettextB="I like APPles";letregex=/apples$/i;// Output: false
console.log(regex.test(textA));// Output: true
console.log(regex.test(textB));
search()
lettext="I like APPles very much";letregexA=/apples/;letregexB=/apples/i;// Output: -1
console.log(text.search(regexA));// Output: 7
console.log(text.search(regexB));
exec()
lettext="Do you like apples?";letregex=/apples/;// Output: apples
console.log(regex.exec(text)[0]);// Output: Do you like apples?
console.log(regex.exec(text).input);
match()
lettext="Here are apples and apPleS";letregex=/apples/gi;// Output: [ "apples", "apPleS" ]
console.log(text.match(regex));
split()
lettext="This 593 string will be brok294en at places where d1gits are.";letregex=/\d+/g;// Output: [ "This ", " string will be brok", "en at places where d", "gits are." ]
console.log(text.split(regex));
lettext="Do you like aPPles?";letregex=/apples/i;// Output: Do you like mangoes?
letresult=text.replace(regex,"mangoes");console.log(result);
replaceAll()
letregex=/apples/gi;lettext="Here are apples and apPleS";// Output: Here are mangoes and mangoes
letresult=text.replaceAll(regex,"mangoes");console.log(result);
Regex in PHP
Functions
-
-
preg_match()
Performs a regex match
preg_match_all()
Perform a global regular expression match
preg_replace_callback()
Perform a regular expression search and replace using a callback
Stringregex="[A-Z\n]{5}$";Stringstr="I like APP\nLE";Patternp=Pattern.compile(regex,Pattern.MULTILINE);Matcherm=p.matcher(str);// Outputs: I like Apple!System.out.println(m.replaceAll("pple!"));
Array of all matches:
Stringstr="She sells seashells by the Seashore";Stringregex="\\w*se\\w*";Patternp=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);Matcherm=p.matcher(str);List<String>matches=newArrayList<>();while(m.find()){matches.add(m.group());}// Outputs: [sells, seashells, Seashore]System.out.println(matches);
Regex in MySQL
Functions
Name
Description
REGEXP
Whether string matches regex
REGEXP_INSTR()
Starting index of substring matching regex (NOTE: Only MySQL 8.0+)
REGEXP_LIKE()
Whether string matches regex (NOTE: Only MySQL 8.0+)
REGEXP_REPLACE()
Replace substrings matching regex (NOTE: Only MySQL 8.0+)
REGEXP_SUBSTR()
Return substring matching regex (NOTE: Only MySQL 8.0+)