Regular expressions (regex) are a powerful tool for pattern matching in strings. They are widely used in programming for tasks like data validation, searching, and parsing. This reference guide will help you understand the basics of regex syntax and usage.

Basic Syntax

Here's a quick overview of the basic syntax elements in regex:

  • . matches any character except a newline.
  • * matches zero or more of the preceding element.
  • + matches one or more of the preceding element.
  • ? matches zero or one of the preceding element.
  • [] defines a character class, matching any one of the characters inside.
  • ^ asserts the position at the start of a line.
  • $ asserts the position at the end of a line.

Examples

Matching Any Character

To match any character, you can use the dot .:

a.c

This regex will match strings like "abc", "axc", "a1c", etc.

Matching Zero or More Characters

To match zero or more of the preceding element, use the asterisk *:

a.*

This regex will match strings like "a", "ax", "axx", "axxx", etc.

Matching One or More Characters

To match one or more of the preceding element, use the plus sign +:

a+

This regex will match strings like "ax", "axx", "axxx", etc., but not "a".

Character Classes

Character classes allow you to match any one of the characters inside the square brackets []:

[abc]

This regex will match any of the characters "a", "b", or "c".

Negation

To match any character except the ones inside the square brackets, use the caret ^:

[^abc]

This regex will match any character except "a", "b", or "c".

Resources

For more detailed information and examples, you can visit our Regex Tutorial.