The regular in this article is represented by Java code
To read this blog first you have to be, someone else gives you a regular expression you have to be proficient in writing Java code, here is a Java regular foundation:
Java Regular basic Usage the difference between \b and \b
Both \b and \b are boundary characters. The difference is that \b is a word delimiter, and \b is a non-word delimiter.
This is also doomed to their matching is different.
the use of \b
Here, the word \b can match Chinese symbols, English symbols, spaces, tabs, carriage return symbols, and various boundaries, such as the word at the beginning and the word at the end.
Here to match the 2 can also be Chinese. It can be handled in Chinese.
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;
public class p3{public
static void main (string args[]) {
string sta= ",,, 2,";
String regex= "\\b2\\b";
Pattern pattern = pattern.compile (regex);
Matcher Matcher=pattern.matcher (STA);
while (Matcher.find ()) {
System.out.println (true);
System.out.println (Matcher.group ());}}
Run Result:
True
2
the use of \b
\b is a non-word delimiter, that is, can detect whether a word, such as "Beidou Wolf God" contains the word "wolf".
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;
public class p2{public
static void main (string args[]) {
string sta= "Beidou wolf God";
String regex= "\\b Wolf \\b";
Pattern pattern = pattern.compile (regex);
Matcher Matcher=pattern.matcher (STA);
while (Matcher.find ()) {
System.out.println (true);
System.out.println (Matcher.group ());}}
Run Result:
True
Wolf
above is the basic usage of \b and \b. considerations for \b and \b
Because \b and \b are boundary-matching characters, it is not possible to determine whether the current string conforms to a certain rule.
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;
public class p1{public
static void main (string args[]) {
string sta= "1";
String regex= "\\b1\\b";
System.out.println (Sta.matches (regex));
}
Run Result:
False
at first I thought the output was true and the result was false .
So \b and \b are generally used to acquire, not to judge and replace.
However, I would like to know why \b can not match the above example ... And not just that it's a boundary match, so if I ask you, what is a boundary character match ... Can you answer, rather than simply recite the concept ...
OK, so look at this example:
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;
public class p1{
the public static void main (string args[]) {
string Sta= "(.? 213lang Wolf 13) I am the gorgeous dividing line Chinese question mark and English question mark.???。 I am the gorgeous line of demarcation space I am the gorgeous dividing line for lines I am gorgeous demarcation tab ";
String regex= "\\b";
String arrays[]=sta.split (regex);
for (String i:arrays) {
System.out.println ("[" +i+ "]");}}
Run Result:
[(.?]
[213lang Wolf 13 I am the gorgeous dividing line Chinese question mark and English question mark]
[。???。 [
I am a gorgeous line of white space] [
] [I am a gorgeous line-
wrapping]
[] [I am
a gorgeous demarcation tab]
[ ]
So is it a revelation. Word boundaries are the boundaries of words and symbols, where the words can be numbers, English words, Chinese words, and these words are not mutually exclusive. The symbols can be English symbols, Chinese symbols, spaces, tabs, newline, and the symbols are not mutually exclusive.
It's because \b match this boundary. So when we want to determine if "1" uses regular rule "\\b1\\b" to match, this is not going to match. because a space is not a boundary, the boundary between a space and a 1 is the \b matching boundary.
so it's only true when we write this
See Example:
public class p4{public
static void main (string args[]) {
string sta= "1";
String regex= "\\b1\\b";
System.out.println (Sta.matches (regex));
}
Run Result:
True
Conversely, the \b boundary is the boundary between the word and the word. This sort of thing just doesn't have to be thought about. Yes, it's wrong. Dear readers.
But let's just take an example.
first I want to declare \b equivalent to [^\b].
See example:
public class p5{public
static void main (string args[]) {
string sta= "123lang Beidou wolf God-,,,,,???。。。 -";
String regex= "\\b";
String arrays[]=sta.split (regex);
for (String i:arrays) {
System.out.println ("[" +i+ "]");}}
Run Result:
[1]
[2]
[3]
[L]
[A]
[N]
[G]
[North]
[Dou]
[Wolf]
[God-]
[,]
[,]
[,]
[,]
[,]
[?]
[?]
[?]
[。 ]
[. ]
[. ]
[-]
Isn't that a little surprising? It's your surprise.
People who are expected to do so don't have to look down, because you're a very smart person.
People who feel surprised, it's no big deal, because the author I think the answer and the answer is not the same.
But no matter what, we all walk, understand ... The path ...
I also stated before the beginning of this example that \b is equivalent to [^\\b]. ^ is the opposite meaning, you can view the API.
Therefore, the \b is the boundary between the word and the symbol. And we are dividing the boundary between the word and the symbol. So the opposite is that the boundary between the word and the symbol is not my boundary, and the boundary between the word and the word and the symbol and the symbol is my boundary.
So in this case there is also a symbol "-" in the "Beidou Wolf God", which means that the boundary between the word and the symbol is not the boundary of the \b, and the boundary between the word and the word and the symbol and the symbol is the boundary of the \b.