Regulations on regular expressions (2)

xiaoxiao2021-03-06  64

Below, we look at how to use the above metades in the regular expression.

/ s /

The above regular expression can be used to match one or more space characters in the target object.

/ d000 /

If we have a complex financial statement in his hand, we can find all the total amount of thousands of yuan through the above regular expressions.

In addition to the metamorphors described above, there is another unique dedicated character, ie, locator in the regular expression. The locator is used to specify the appearance of the matching mode in the target object.

More commonly used locators include: "^", "$", ",", "b". Where "^" positioning specifies that the matching mode must appear at the beginning of the target string, the "$" locator specifies that the matching mode must appear in the end of the target object, the positioner specifying the matching mode must appear on the beginning or end of the target string. One of the two boundaries, and the "B" positioning runation specifies that the matching object must be within two boundaries of the beginning and end of the target string, ie the matching object can neither act as the beginning of the target string, and cannot be used as a target string. The end of the end. Similarly, we can also regard "^" and "$" and "b" as two sets of locators that are mutually counters. for example:

/ ^ Hell /

Since the above regular expression contains "^" locator, it can match the string of "Hell", "Hello" or "Hellhing" in the target object.

/ AR $ /

Since the "$" locator is included in the above regular expression, it can match the string ends with "car", "bar" or "ar" in the target object.

/ BOM /

Since the above regular expression mode is in the "" positioner, it can match the string of "BOMB" or "BOM" in the target object.

/ MAN /

Since the above regular expression mode is matched with the target object in the target object, it can match the string of "Human", "Woman" or "Man" in the target object.

In order to facilitate user more flexible setting matching mode, the regular expression allows the user to specify a range in the match mode without being limited to the specific character. E.g:

/ [A-z] /

The above regular expression will match any uppercase from the A to Z.

/ [a-z] /

The above regular expression will match any lowercase alphabet from the A to Z.

/ [0-9] /

The above regular expression will match any of the numbers from 0 to 9.

/ ([A-Z] [A-Z] [0-9]) /

The above regular expression will match any string consisting of letters and numbers, such as "AB0". Here, it is necessary to remind the user to pay attention to the use of "()" to combine the string in the regular expression. "()" The content containing the symbol must appear in the target object at the same time. Therefore, the above regular expression will not match a string such as "ABC", because the last character in "ABC" is a letter rather than a number.

If we want to implement "or" or "operations in the regular expression, you can use a match in multiple different modes to use the pipeline" | ". E.g:

/ to | TOO | 2 /

The above regular expression will match "TO", "TOO", or "2" in the target object.

There is also a more common operator in the regular expression, ie, negative "[^]". Unlike the locator "^" described in our forebel, negative "[^]" specifies the string specified in the mode in the target object. E.g:

/ [^ A-c] /

The above strings will match any characters other than A, B, and C in the target object. In general, when "^" appears in "[]", it is considered a negative operator; and when "^" is "[]", or "[]", it should be regarded. Locator.

Finally, when the user needs to add a metammat in the regular expression of the regular expression, and find the escape character when it looks for match objects. E.g:

/ TH * /

转载请注明原文地址:https://www.9cbs.com/read-86195.html

New Post(0)