Rules of the Regular Expression Engine

Source: Internet
Author: User
Tags expression engine

Rule 1: The Regular Expression Engine Starts matching the input string as soon as possible. It searches for one character at a time until it finds a match.

For example:

String = 123 ABC 456 def

RegEx = [A-Z] *

Matching starts from ABC.

Rule 2: After a matching start is found, the Regular Expression Engine continues to match until a character is received in the mode.

For example:

String = 123 ABC 456 def

RegEx = [A-Z] *

The first match starts with ABC and ends when a space is not accepted by the mode.

Rule 3: The RegEx engine is greedy-as long as the mode permits, it will match as many characters as possible.

For example:

String = 'Dr Watson's watch'

RegEx = '.*'

Here we will match 'Dr Watson's Watch'. If the mode is changed '.*? ', Is "*" to become a non-Greedy qualifier. This will match 'Dr watson'

Rule 4: The RegEx engine is eager to implement matching, so it will backtrack the request to implement matching.

For example:

String = 'Hello world' s said K & R.

RegEx = '.*'

Match 'Hello world' here '.

If you set the mode to a non-backtracing "'(?>. *) '"Then no matching is found. Because ". *" matches all characters on the right until the end, and does not use backtracing, it cannot be matched.

Rule 5: The RegEx engine always selects the first option.

For example:

String = 1234 123 3456

RegEx = (\ D {2} | \ D {3} | \ D {4 })

The matching result is as follows:

12

34

12

34

56

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.