Regular Expression concise Learning (1)

Source: Internet
Author: User

 

Waht

Regular expressions describe some rules, and these rules are used to search for and match strings by specifying the appearance mode of their leading characters (that is, the characters before the metacharacters) in the target object,

hi 

This simple string is a regular expression for exact matchinghiThis word, but we not only need to find thishi, You also need to find a separatehiOr containhiThe metacharacter)

Metacharacters

Metacharacters are special codes specified by regular expressions. They are replaced by one or a set of specified characters.Some Characters, The specified characters here are similar*,.,\b..., AndSome CharactersIt may be a special character, like a space '', or a segment of any character. First, a table is created and then interpreted.

Common metacharacters Description
* Does not match any character. It indicates the mode that appears for any number of times. It is used to modify the leading character.
. Match any character except linefeed
\ W Match letters, numbers, underscores, or Chinese Characters
\ S Match any blank space character
\ D Matching number
\ B Start or end of a matching word
^ Start of matching string
$ End of matching string
.& *

Right?*It's strange. How many times can we combine it.Put it together for consideration,.Match any character except the line break.*Match strings of any length that do not contain line breaks

\b

In English, space is used as a marker to separate words, while \ B matches the position of such a word.

For example: \bhi\b.*\bword\b

Look for a sentence that contains the word "hi" and "word ".

####\d
Match a number. Yes, it only matches a number.

For example: 0\d\d-\d\d\d\d\d\d\d\d

It starts with 0, followed by two numbers, followed by a hyphen (-), and finally eight numbers.

For example: \b\w{6}\b

Match a word with exactly 6 Characters and get the code,\wMatching things characters, numbers, and underscores are all necessary elements of a word. Therefore, it is generally used to match a word.{8}Is the modifier of the leading character, andSimilarly, it indicates exactly how many times it matches.

^ $

^Used for matching searchStringStart
$Used for matching searchStringEnd
The two and\bSimilarly, it is used to match a location of a string, rather than a character.

For example: ^\d{5,12}$

Matching a number ranging from 5 to 12

Character escape

If you have learned C or any language, they all have the function of using specific characters to control the output. to output these specific characters, they will use character escaping, regular Expressions also determine a character escape rule for metacharacters
Add\For example\Change\\,.Change\., And so on

Repeated

Here, repetition is not a single repetition of a specific character, but also a repetition of different characters.

Code/syntax Description
*? Repeat any time, but as few as possible
+? Repeat once or more times, but as few as possible
?? Repeated 0 or 1 times, but as few as possible
{N, m }? Repeat n to m times, but as few as possible
{N ,}? Repeated more than N times, but as few as possible

### Next instance:Windows\d+

Match one or more numbers after windows

Character class

To control and express more characters, a character class is introduced. It is a character set combination and uses[,]Include it.
Act as a tuples in a regular expression.
For example[.?!]Match punctuation marks (. Or? Or !),[aeiou]Match any English vowel.

 

Regular Expression concise Learning (1)

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.