Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of\ B and\ b in regular expressions

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of\ B and\ b in regular expressions. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

There are some things that you don't understand about\ B and\ b of regular expressions.

Maybe you can get to know more about\ B and\ b by reading the following blog.

According to the view of API, we can see that\ B and\ b are boundary matches.

Let's talk about the boundary of the word\ b first. Unexpectedly, if you want to understand, you must first know what the word boundary is! We can explore it with\ b as a partition.

Word boundary

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= "(Chinese question mark? 123??? English) question mark? I am gorgeous [tabs\ t] I am gorgeous {spaces I am the newline character of gorgeous}\ n "; String rex="\\ b "; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); String [] result=pattern.split (str); for (String string:result) {System.out.println (" split string: "+" ["+ string+"]);}

Running result

Split string: [(]

Segmented string: [Chinese question mark]

Split string: [?]

Split string: [123]

Segmented string: [?]

Segmented string: [English]

Split string: [)]

Split string: [question mark]

Split string: [?]

Split string: [I am gorgeous]

Split string: [[]

Segmented string: [tabs of]

Split string: []]

Split string: [I am gorgeous]

Split string: [{]

Segmented string: [space character]

Split string: []

Split string: [I am gorgeous]

Split string: [}]

Split string: [newline character of]

Split string: [

]

From these segmented strings, we can know that the word boundary is the boundary between words and symbols.

The words here can be Chinese characters, English characters, numbers; symbols can be Chinese symbols, English symbols, spaces, tabs, line breaks

Let's look at an example.

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= "2"; String rex= "\\ b2\\ b"; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); if (matcher.matches ()) {System.out.println ("match succeeded");} else {System.out.println ("match not successful");}

Before looking at the above split example, it is estimated that many people, including me, will think that the result of this operation is a successful match.

After the split example, we know that the space is not the boundary space between the boundary space and the number 2, so the running result is self-evident that the match must not be successful.

If you write like this and run it, it will be a success.

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= "2"; String rex= "\\ b2\\ b"; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); if (matcher.matches ()) {System.out.println ("match succeeded");} else {System.out.println ("match not successful");}

The usage of\ b

Generally speaking,\ b is not used to judge whether the current string conforms to a certain rule.

Usually we use\ b to get.

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= ", hehe,"; String rex= "\\ b hehe\\ b"; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); if (matcher.find ()) {System.out.println (matcher.group ());}

Running result

Hehe 1

The usage of\ B

Now that we know the usage of\ b, let's talk about whether\ B\ B is not a word boundary.

That is to say,\ B = [^\ b] / / the symbol ^ means right and wrong.

\ b is the boundary between words and symbols, and everything else that is not the boundary between words and symbols is\ B.

So our guess is that\ B is the boundary between symbols and symbols, words and words.

Of course, the guess needs to be certified! Let's write an example to prove one!

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= "123456 I am JAVA {,; 'asd"; String rex= "\ B"; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); String [] result=pattern.split (str); for (String string:result) {System.out.println ("segmented string:" + string);}

Running result

Split string: 1

Split string: 2

Split string: 3

Split string: 4

Split string: 5

Split string: 6

Segmented string: I

Split string: yes

Split string: J

Split string: a

Split string: v

Segmented string: the boundary between A {/ / words and symbols is not the boundary of\ B.

Split string:

Segmented string:,

Split string:

Split string:'a

Split string: s

Split string: d

It turns out that\ B, as a non-word boundary, is indeed a boundary between words and words, symbols and symbols.

\ B is also used to get strings.

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class matcher1 {public static void main (String [] args) {String str= ", and hehe,"; String rex= "\\ B\ B"; Pattern pattern=Pattern.compile (rex); Matcher matcher=pattern.matcher (str); if (matcher.find ()) {System.out.println (matcher.group ());}

Because the boundary between characters

So the result of the operation is

Oh.

Add:

What's the difference between\ b and\ s in regular expressions?

String: abcsdsadas abc asdsadasdabcasdsa

Use\ sabc\ s and\ babc\ b to match the middle abc, isn't this method repeated, / s matches spaces, enter, etc. / b online tutorials don't know what character boundaries mean.

Different

\ babc\ b matches "abc"

\ sabc\ s does not match "abc" with a space "abc" before and after it.

\ b only matches the position of the beginning and end of the string and the carriage return of the space, not the space character itself.

For example, "abc sdsadasabcasdsadasdabcasdsa"

\ sabc\ s cannot match,\ babc\ b can match to "abc"

\ b stands for the invisible thing between words, such as

Here is a word

So, there are several\ b in this sentence, and there is one before and after each word.

So you can match the above here with\ bhere\ b, but if here is not a word, but part of a word, such as adheread, then it can be matched with here, but not with\ bhere\ b, because there is no\ b after ad. So the here in adhere will not be matched.

Conclusion:\ b is used when you match the whole word. If it's not the whole word, it doesn't match. If you want to match I, you know, a lot of words have I, but I just want to match I, which is "I", and use\ bI\ b at this time.

B is the other way around, which means not between words. Something like\ d represents a number and\ D represents a non-number.

This is the end of the article on "sample Analysis of\ B and\ b in regular expressions". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report