Basic Pattern Matching
Everything starts from the most basic. Patterns are the most basic elements of formal expressions, which are a set of characters that describe the character of a string. Patterns can be simple, consist of ordinary strings, or can be very complex, often with special characters representing a range of characters, repeating, or representing context. For example:
^once
This pattern contains a special character ^, which indicates that the pattern matches only those strings that begin with once. For example, the pattern matches the string "Once Upon a Time" and does not match "there once is a man from NewYork". Just as the ^ symbol represents the beginning, the $ symbol is used to match strings that end in a given pattern.
bucket$
This pattern matches the "who kept all of the cash in a bucket" and does not match "buckets". The characters ^ and $ are used together to indicate exact matches (the string is the same as the pattern). For example:
^bucket$
Matches only the string "bucket". If a pattern does not include ^ and $, then it matches any string that contains the pattern. Example: Mode
Once
With string
There once is a man from NewYork
Who kept all the cash in a bucket.
is a match.
The letters in the pattern (O-N-C-E) are literal characters, that is, they represent the letter itself, and the numbers are the same. Some other slightly more complex characters, such as punctuation and white characters (spaces, tabs, etc.), are used to escape sequences. All escape sequences begin with a backslash (\). The escape sequence for a tab is: \ t. So if we're going to check if a string starts with a tab, you can use this pattern:
^\t
Similarly, use \ n to indicate "new line" and \ r for carriage return. Other special symbols can be used in front with a backslash, such as the backslash itself with \ \, period. Use \. To indicate, and so on.