Regular expression \b

Source: Internet
Author: User

Quote a Passage online:

\b is a special code prescribed by regular expressions (well, some people call it metacharacters, metacharacter), which represents the beginning or end of a word, that is, the boundary of a word. Although English words are usually delimited by spaces, punctuation marks, or line breaks, \b does not match any of these word-delimited characters, it only matches one position.

If a more precise argument is needed, \b matches such a position: its previous character and the next one are not all (one is, one is not or does not exist) \w.

Many people do not understand the meaning of the \b in the regular, after seeing the above paragraph, many people still do not understand exactly what a "position" of \b.

Today is to say what I understand.

What is location

It ' s a nice day today.

' I ' occupies a position, ' t ' occupies a position, and all individual characters (including invisible white space characters) will occupy a position where I give it a name called "explicit position".

Note: There is also a position between the character and the character, for example ' I ' and ' t ' have a position (nothing), so I give it a name called "Implicit position".

"Implicit location" is the key to \b! Popular understanding, \b is "implicit position".

At this point, let's try to understand this sentence:

If a more precise argument is needed, \b matches such a position: its previous character and the next one are not all (one is, one is not or does not exist) \w.

I use my words to translate this sentence:

The "implicit location" \b, which matches this position: its previous "explicit position" character and the subsequent "explicit position" characters are not all \w.

Now, is there a sense of enlightened feeling? Do you have one? Do you have one? Do you have one?

Example Explanation

Just use "It's a nice day today." For example:

Correct regularization: \bnice\b

Analysis: The first \b in front of a character is a space, followed by a character is ' n ', not all \w, so you can match the ' n ' is the beginning of a word. The second \b before a character is ' E ', the following character is a space, not all \w, can match the ' e ' is the end of a word. So, together, you can match a word that ends with ' e ' starting with ' n ', which matches the word "nice".

Incorrect regex: A\bnice

Analysis: I have seen someone like this to write the regular, want to achieve the purpose is to match the last word with ' a ' end, the next word with ' n ' part, here want to match "a nice." But this is not the purpose of the regular expression, \b Front is the character ' A ', followed by the character ' N ', two are "explicit characters", obviously contrary to the meaning of \b, so this is a wrong expression, can not match anything. To match "A nice", the correct regular notation is: A\b.\bnice (cannot wrap)

Http://www.cnblogs.com/litmmp/p/4925374.html

Regular expression \b (rpm)

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.