In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Java regular expression usage examples, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
A regular expression is a specification that can be used for pattern matching and substitution. a regular expression is a text pattern consisting of ordinary characters (such as characters a to z) and special characters (metacharacters). It is used to describe one or more strings to match when looking for the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.
As we all know, in program development, it is inevitable to encounter situations that need to match, find, replace and judge strings, and these situations are sometimes more complex, if solved by pure coding, it is often a waste of programmers' time and energy. Therefore, learning and using regular expressions has become the main means to solve this contradiction.
As we all know, a regular expression is a specification that can be used for pattern matching and substitution. a regular expression is a text pattern consisting of ordinary characters (such as characters a to z) and special characters (metacharacters). It is used to describe one or more strings to match when looking for the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.
Since jdk1.4 launched the java.util.regex package, it has provided us with a good JAVA regular expression application platform.
Because regular expressions are a very complex system, I will only give some examples of getting started. For more information, please refer to the relevant books and explore by yourself.
/ / backslash
/ t interval ('/ u0009')
/ n line feeds ('/ u000A')
/ r enter ('/ u000D')
The / d number is equivalent to [0-9]
/ D is non-numeric equivalent to [^ 0-9]
/ s blank symbol [/ t/n/x0B/f/r]
/ S non-blank symbol [^ / t/n/x0B/f/r]
/ w separate character [a-zA-Z_0-9]
/ W is not a separate character [^ a-zA-Z_0-9]
/ f feed character
/ e Escape
/ b the boundary of a word
/ B the boundary of a non-word
/ G the end of the previous match
^ is the beginning of the restriction
The ^ java condition is limited to characters that begin with Java
$is the end of the limit
The java$ condition is limited to characters ending with java
. Conditional restriction on any single character except / n
Java.. The condition is limited to any two characters except newline after java
Add specific restrictions "[]"
[Amurz] condition is limited to one character in the lowercase a to z range
[Amurz] condition is limited to one character in uppercase A to Z range
[a-zA-Z] the condition is limited to one character in the range of lowercase a to z or uppercase A to Z
[0-9] the condition is limited to one character in the lowercase 0 to 9 range
The [0-9a-z] condition is limited to one character in the lowercase 0 to 9 or a to z range
The [0-9 [a to z]] condition is limited to one character (intersection) in the range of lowercase 0 to 9 or a to z
Add ^ to [] and add another constraint "[^]"
[^ aMurz] condition is limited to one character in non-lowercase a to z range
[^ Amurz] condition is limited to one character in non-uppercase A to Z range
[^ a-zA-Z] condition is limited to one character in the range of non-lowercase a to z or uppercase A to Z
[^ 0-9] condition is limited to one character in the range of non-lowercase 0 to 9
[^ 0-9a-z] condition is limited to one character in the range of non-lowercase 0 to 9 or a to z
[^ 0-9 [amurz]] condition is limited to one character (intersection) in the range of non-lowercase 0 to 9 or a to z
When the restriction is that a specific character appears more than 0 times, you can use "*"
J* more than 0 J
. * more than 0 arbitrary characters
There are more than 0 arbitrary characters between J and D.
You can use "+" when the restriction is that a specific character appears more than once.
J + 1 or more J
. + 1 or more arbitrary characters
More than 1 arbitrary character between J.D.J and D.
You can use "?" when the restriction is that a specific character appears 0 or more times.
JA? J or JA appears.
Limit the occurrence of the character "{a}" to a specified number of times in a row
J {2} JJ
J {3} JJJ
More than a text, and "{a,}"
J {3,} JJJ,JJJJ,JJJJJ,??? (more than 3 times J coexist)
More than one text, and less than b "{arecom b}".
J {3cine 5} JJJ or JJJJ or JJJJJ
One of the two is "|"
J | A J or A
Java | Hello Java or Hello
A combination type is specified in "()"
For example, when I query the data in the middle of index, I can write (. +?)
When using the Pattern.compile function, you can add parameters that control the matching behavior of regular expressions:
Pattern Pattern.compile (String regex, int flag)
The value range of flag is as follows:
Pattern.CANON_EQ determines a match if and only if the normal decomposition (canonical decomposition) of the two characters are exactly the same. For example, after using this flag, the expression "a/u030A" matches "?". By default, gauge equivalence (canonical equivalence) is not considered.
Pattern.CASE_INSENSITIVE (? I) by default, case ambiguity matches only apply to the US-ASCII character set. This flag allows expressions to match regardless of case. To match Unicode characters with unknown size, simply combine UNICODE_CASE with this flag.
Pattern.COMMENTS (? X) in this mode, the space character (in the regular expression) is ignored when matching. (translator's note: not the "/ / s" in the expression, but the space in the expression, tab, carriage return, etc.). The comment starts at # and ends at the end of the line. Unix line mode can be enabled with embedded flags.
Pattern.DOTALL (? s) in this mode, the expression'.' You can match any character, including the Terminator that represents a line. By default, the expression'.' The Terminator that does not match the line.
Pattern.MULTILINE
(? M) in this mode,'^ 'and' $'match the beginning and end of a line, respectively. In addition,'^ 'still matches the beginning of the string, and' $'also matches the end of the string. By default, these two expressions only match the beginning and end of the string.
Pattern.UNICODE_CASE
(U) in this mode, if you also enable the CASE_INSENSITIVE flag, it will match the case of Unicode characters with ambiguity. By default, case-insensitive matches apply only to the US-ASCII character set.
Pattern.UNIX_LINES (? d) in this mode, only'/ n' is considered a stop of a line and matches'.','^', and'$'.
Despite the vague concepts, here are a few simple Java regular use cases:
◆, for example, when a string contains validation
/ / find the string Pattern pattern = Pattern.compile ("^ Java.*") that starts with Java and ends arbitrarily; Matcher matcher = pattern.matcher ("Java is not a human"); boolean b = matcher.matches (); / / returns true when the condition is met, otherwise false System.out.println (b)
When ◆ splits a string with multiple conditions
Pattern pattern = Pattern.compile ("[, |] +"); String [] strs = pattern.split ("Java Hello World Java,Hello,World | Sun"); for (int item0)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.