Home  >  Article  >  Backend Development  >  Various regular expression examples usage

Various regular expression examples usage

巴扎黑
巴扎黑Original
2017-04-20 18:01:551682browse

Select

Enclose all selections in parentheses, and separate adjacent selections with |. But using parentheses will have a side effect, that is, related matches will be cached. In this case, you can use ?: before the first option to eliminate this side effect.

Among them, ?: is one of the non-capturing elements, and the other two non-capturing elements are ?= and ?!. These two have more meanings. The former is a forward lookup and matches at any beginning. The search string is matched at any position within the regular expression pattern within parentheses, which is a negative lookahead that matches the search string at any initial position that does not match the regular expression pattern.

Backreference

Adding parentheses around a regular expression pattern or part of a pattern will cause the associated match to be stored in a temporary buffer, with each captured Submatches are stored from left to right as encountered in the regular expression pattern. The buffers in which submatches are stored are numbered starting from 1 and numbered consecutively up to a maximum of 99 subexpressions. Each buffer can be accessed using ‘\n’, where n is a one- or two-digit decimal number that identifies the specific buffer.

The non-capturing metacharacters ‘?:’, ‘?=’, or ‘?!’ can be used to ignore the preservation of related matches.


Operation priority of various operators

Those with the same priority are performed from left to right Operations, operations with different priorities are higher first and then lower. The precedence of various operators is as follows from high to low: Escape character

(), (?:), (?=) .                          position and Sequence

| Regular expression                                                          The position where a word appears continuously

/(\w+):\/\/([^ /:]+)(:\d*)?([^# ]*)/ Parse a URL into protocol, domain, port and relative path

/^(?:Chapter|Section) [1 -9][0-9]{0,1}$/ Locate the position of the chapter

/[-a-z]/                                                                                                                                                                                                                                                                    .

/ter\b/                                                                                                                                                                                                                                                                                                         

Can match Windows95 or Windows98 or WindowsNT. When a match is found, start from behind Windows next time

The above is the detailed content of Various regular expression examples usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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