Files
Blog_and_Personal_Page/content/knowledge-base/regex-basics.md
T
2024-12-03 15:38:07 +01:00

58 lines
1.8 KiB
Markdown

+++
date = '2022-09-04T15:43:06'
draft = false
title = 'Regex Basics'
type = 'post'
tags = ['Misc']
+++
[just text:](#just-text) matches any string with the given text inside it
[^](#^) matches string that starts with the given text
[$](#$) matches string that ends with the given text
[*](#*) zero or more
[+](#+) one or more
[?](#?) zero or one
[{n}](#{n}) `n` occurrences
[a|b or [ab]](#OR-operator) a or b
[\\d](#\\d) matches a digit
[\\w](#\\w) matches a word character (alphanumeric plus underscore)
[\\s](#\\s) matches a whitespace character
[.](#.) matches any character
## Anchors
### just text
`abc` matches any string that has the text _abc_ in it
### ^
`^abc` matches any string that starts with _abc_
### $
`abc$` matches any string that ends with _abc_
### exact match
`^abc$` matches only the exact string _abc_
## Quantifiers
### *
`abc*` matches any string containing _ab_ followed by any number of _c_ s
### +
`abc+` matches any string containing _ab_ followed by at least one _c_
### ?
`abc?` matches any string containing _ab_ followed by at the most one _c_
### {n}
`abc{2}` matches any string containing _ab_ followed by two _c_ s
`abc{2,}` matches any string containing _ab_ followed by two __or more__ _c_ s
`abc{2,5}` matches any string containing _ab_ followed by between two and five _c_ s
## OR operator
`a|b` _a_ or _b_
`[abc]` _a_ or _b_ or _c_
## Character classes
### \d
`\d` matches any character in a string that is a digit
`\D` matches any character that __isn't__ a digit
### \w
`\w` matches any character in a string that is a word character
`\W` matches any character that __isn't__ a word character
### \s
`\s` matches any whitespace character in a string
`\S` matches any __non__-whitespace character
### .
`.` matches any character in a string