Regular Expression (Regex)
A sequence of characters that specifies a search pattern in text. Used by string-searching algorithms for 'find' or 'find and replace' operations.
A Regular Expression (Regex or Regexp) is a sequence of characters that specifies a match pattern in text. Usually, such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
Regex is extremely powerful for parsing logs, validating user input (like emails or phone numbers), and refactoring code.
Basic Syntax
While different programming languages might have slightly different regex "flavors" (e.g., PCRE, JavaScript, Python), the core concepts remain identical:
.- Matches any single character except newline.\w- Matches any word character (alphanumeric & underscore).\d- Matches any digit (0-9).\s- Matches any whitespace character (space, tab, newline).[abc]- Character class. Matches any character within the brackets (a, b, or c).[^abc]- Negated character class. Matches any character not in the brackets.^- Anchors the match to the beginning of the string (or line).$- Anchors the match to the end of the string (or line).
Quantifiers
Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.
*- Zero or more times.+- One or more times.?- Zero or one time (optional).{n}- Exactly n times.{n,}- n or more times.{n,m}- Between n and m times.
Example
A common regular expression to validate a simple email address looks like this:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Try the Tool
DevFlow provides free, privacy-first tools related to Regular Expression (Regex). They run completely in your browser.