Understanding of \b in regular expressions
\b is used to set word boundaries. Matches the beginning or end of a word (composed of letters, numbers or underscores) (when the match starts, there cannot be \w before the word; when the match ends, there cannot be \w after the word). If written before a certain character or string in the expression, it means that there cannot be a character corresponding to \w before this character or string; if written after a character, it means that there cannot be a character corresponding to \w after this character or string. Therefore, only one \b can be placed before or after the character, or there can be two (meaning that there cannot be a character corresponding to \w before or after the character).
Look at the correct situation first
Figure 1
Note: 1. If the description of \b does not consist of letters, numbers or underscores word, the expression is misdefined and no string will ever match it. As shown in Figure 2:
Figure 2
2. The target string matches only individual words, excluding the preceding and following words. Spacing (such as spaces, \W, etc.), The spaces on both sides of the matched "Russell" character in Figure 1 are not counted (the test tool shows no blue background filling).
Purpose: Match independent words or parts of strings. The business rule is to find all places where the word "Russell" appears independently. For example, the expression \bRussell\b means Russell123abc does not match, because Russell should not be followed by letters, numbers, or underscores. Russell 123abc and Russell@123abc both match.
3.Special circumstances. Contradictory expression definition.
\b is only used to limit words consisting of letters, numbers or underscores,
If there are other expressions after the expression\b , then the expression after the expression \b must not be \w or cannot be the content in \w, because
example, the expression \bhi\bnihao, means that there cannot be the content of \w before and after the word hi, and It is required that the target string has hi, and hi is followed by the "nihao" character. That is, the definition of this expression is self-contradictory.
Because: according to the definition of \b, \b only ensures that the target string can match the following conditions: the characters before and after \b cannot have \w, so non-\w (or \W) characters before and after \b will match. Note that it is required here that if you want to match a non-\w character, the subtext of the regular rule has already said: the character matching \b must be surrounded by a non-\w character, so you must add a non-\w character after the \b expression. Only with other expressions of \w can the target string be matched.
So a regular expression like this will never be matched: \bhi\bnihao Target string hinihao hi nihao hi*nihao hi @#$nihao . . .
Because \bhi\b requires that there cannot be alphanumeric or underline characters before and after hi, and hi must be followed by the nihao string. So there can be spaces before and after hi! Special characters such as @#¥%, and nihao is defined immediately after hi in the expression, so there will never be a target string that matches this regular expression.
Method: When defining the regular expression, take this non-\w regular expression into consideration (write it into the expression) , and modify the regular expression to \bhi\b \W+nihao then has
hinihao
hi nihao
hi@nihao
hi!@#$ nihao
The last three all match
The above is the detailed content of Understanding of \b in regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used

Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

Regular expressions are a powerful tool for matching strings and can facilitate string manipulation. However, in the process of writing regular expressions, sometimes you may need to match some special characters, such as "\", "|", "{", etc. These characters have special meanings in regular expressions and need to be escaped.
