Useful regular expressions

Useful regular expressions

Task descriptionPHP ExampleMySQL ExampleJava Example
Search for words regardless order in a sentense/(?=.*?\brese_no=(\d*)\b)(?=.*?\bapp_code=((\w|\d)*)\b).*/
Search any word except the specified one. For example:
"Hi Vlad!" - matches
"Hi Guest!" - doesn't match
/^Hi\s+(?!(?:Guest))\w+!$/select
*
from
table
where
name NOT REGEXP 'Guest'
Easy email address validation/^\S+@\S+\.\S+$/
Repeating constructions. For example sum of money in "1,111,223,564.28" format/^(?:\d{1,3}\,\d{1,3})+\.\d{2}$/
Positive sum of money validation in "110101.00" format/^[^0]\d{0,12}(?:\.\d{2})?$/
Search for string of minimal length. For example a string of length 25 symbols not containing whitespace or tab/[^\s\t]{25,}/
Get a domain name from URL/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/
Validate file extension/\.(jpe?g|png|gif|bmp)$/
Space symbolpreg_replace("/[[:blank:]]+/"," ",$str);body.replaceAll("\\p{Blank}+", " ")
nbsp html symbolpreg_replace('/\s \s/ig', ' ', $text);