Regular expression matching rules

巴扎黑
Release: 2023-03-07 15:24:01
Original
1792 people have browsed it

Basic Pattern Matching

Everything starts from the basics. Patterns are the most basic elements of regular expressions. They are a set of characters that describe the characteristics of a string. Patterns can be simple, consisting of ordinary strings, or very complex, often using special characters to represent a range of characters, recurrences, or to represent context. For example:

  ^once

This pattern contains a special character ^, which means that the pattern only matches those strings that begin with once. For example, this pattern matches the string "once upon a time" but does not match "There once was a man from NewYork". Just like the ^ symbol indicates the beginning, the $ symbol matches strings that end with a given pattern.

bucket$

This pattern matches "Who kept all of this cash in a bucket" but does not match "buckets". When the characters ^ and $ are used together, they represent an exact match (strings are the same as patterns). For example:

^bucket$

Only matches the string "bucket". If a pattern does not include ^ and $, then it matches any string that contains the pattern. For example: pattern

once

and string

There once was a man from NewYork

Who kept all of his cash in a bucket.

is matched.

The letters (o-n-c-e) in this pattern are literal characters, that is, they represent the letters themselves, and the same goes for numbers. Other slightly more complex characters, such as punctuation and white characters (spaces, tabs, etc.), require escape sequences. All escape sequences begin with a backslash (\). The escape sequence for the tab character is: \t. So if we want to detect whether a string starts with a tab character, we can use this pattern:

  ^\t

  Similarly, use \n to represent "new line" and \r to represent "new line" Enter. Other special symbols can be used with a backslash in front. For example, the backslash itself is represented by \\, the period is represented by \., and so on.

The above is the detailed content of Regular expression matching rules. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!