In YAML, regular expressions (regex) are typically used for pattern matching within strings. Below is a cheat sheet with some commonly used regex patterns in the context of YAML:
Basic Patterns
.
(dot):- Matches any single character, except for a newline.
*
:- Matches 0 or more occurrences of the preceding character.
+
:- Matches 1 or more occurrences of the preceding character.
?
:- Matches 0 or 1 occurrence of the preceding character.
Character Classes
[ ]
:- Defines a character class.
[^ ]
:- Defines a negated character class.
Escape Characters
\
:- Escapes a special character.
Common Metacharacters
\d
:- Matches any digit (equivalent to
[0-9]
).
- Matches any digit (equivalent to
\D
:- Matches any non-digit.
\w
:- Matches any word character (alphanumeric + underscore).
\W
:- Matches any non-word character.
\s
:- Matches any whitespace character.
\S
:- Matches any non-whitespace character.
Multi-Line Patterns
YAML regex patterns are often used to match specific patterns across multiple lines. For multiline patterns, you may need to consider using the (?s)
flag to make .
match newline characters.
Online YAML Regex Testers
You can use online regex testers to experiment and test your YAML regex patterns. Some popular online regex testers include Regex101 and RegExr.
Remember that YAML itself does not have native support for regex; it depends on the context in which you are using YAML data and the programming language or tool you are using to process it. Adjust the patterns based on your specific needs and the regex flavor supported by your environment.