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--
Today, I will talk to you about the role of regular expressions in java, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Determine whether this character may be contained in the target string.
True if the string to be matched contains the specified string and matches the regular expression, and true if the string to be matched does not contain the specified string but matches the regular expression, if otherwise false.
Look at a simple code example:
Private static String s = "1"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("1?"));}
This program tests whether s contains "1", returns true if it does, and false if it doesn't. The result of running the program here is: true.
Pay attention to this? The usage of contains is different from that of contains, which is used to test whether a string is included in a string, and the parameter after match is the regular form of the entire string.
You can do another simple test:
Private static String s = "1java"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("1?"));}
Here, the string to be matched is changed to "1java", and the result displayed by running this program is false. At this point, we can modify it a little bit again:
Private static String s = "1java"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("1? [a Murz] +");}
The above code modifies the parameters in match, and the result is true.
Let's take another look at the situation.
Private static String s = "12"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("0?\\ d +");}
At this point, the result returned is still true, and the string to be matched does not contain "0", but the following expression "\ d +" exactly matches "12". So return true.
2. The function of "\"
Escape character
In the use of String in java, we know that "\" indicates an escape character. When we need to show
String s = "He is a" Monster ""
The "" cannot be written directly into the string, otherwise the compiler will report an error directly. It needs to be converted by an escape character:
String s = "He is a\" Monster\ ""
However, in regular expressions in java, it is sometimes necessary to use "\" to represent specific symbols, such as\ d to match a digit in a regular expression, but in a regular expression, a double backslash such as\ must be used to represent a\. In other words, we have to write\ d in the code to match an array, which is equivalent to\ d in a regular expression.
If you want to insert a normal\ into the regular expression, you need to write\.
Private static String s = "\\ 12"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s); System.out.println (s.contains ("\\")); System.out.println (s.matches ("\?\\ d +"));}
Notice the representation in this program: s =\ 12, this is our string to be matched, which contains a\ and two digits 12. The String.contains () method can match a string and can use the normal\\ representation method, but in the String.match () method it is a regular expression, and you must use\ to represent a normal\ in order to match.
3. The function of "+"
One or more previous expressions
The function of this character has been explained slightly earlier.\ d means to match a number, and adding a "+" after it means to match one or more digits.
Private static String s = "12345"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("\?\\ d +"));}
No matter how many numbers are contained in s, they can be matched as long as they are all numbers. To match 123, you need to use the following expression:
(123) +
There is a mistake in writing as follows:
123 +
This representation matches 12 first, and then multiple 3s. Code testing:
Private static String s = "1233"; public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s.matches ("123 +"));}
If the output is true, this is the matching 12 + multiple 3s.
Quantifier
A quantifier indicates the pattern of a regular expression in the process of matching.
Greedy general matching patterns are always greedy unless other options are set. Greedy expressions match as much as possible for all possible matches, that is, the string that matches the most.
Reluctantly, the expression will match as little as possible, that is, the string with the least number of matches.
This type of possession is unique to java.
The greedy barely possessive type means that one or more XX*X*?X*+0 {n} X {n}? X {n} + exactly n times XX {n,} X {n,}? X {n,} + at least n times XX {n ~ m} X {n ~ m}? X {n ~ m} + at least n times
Characters in regular expressions
Character
The following table shows some commonly used character representations
The example shows that B specifies the character B\ xhh16 base to represent the character, and the 0xhh\ uhhhh16 base represents the unicode character 0xhhhh\ t tab TAB\ n line feed\ r carriage return\ f page feed\ e escape (escape)
Character class
The following table lists the character classes commonly used in regular expressions
The example shows. Matching any single character [abc] contains any character in abc is equivalent to a [^ ABC] any character other than abc [a-zA-Z] any character from a to z or from A to Z [abd [1-9]] abd or any character in 1-9, take and merge [a Mustang & [hij]] any h, I, j character Take intersection\ s blank characters (space, tab, line feed, page feed and enter)\ S non-blank character (^\ s)\ d number (0-9)\ D non-numeric character (^ 0-9)\ w word character [a-zA-Z0-9]\ W non-word character [^\ w]
Logical operator
The example shows that XYY follows X with XY (X) capture group
Boundary match character
An example illustrates the beginning of a ^ line, the end of a line\ b the boundary of a word\ B the boundary of a non-word\ G the end of the previous match
A simple example to create the regular expression in the table above
Private static String s = "java123\ nJAVA"; / / the form is "[arelz] {4} [0-9] {3}\ s [A-Z] {4} public static void main (String [] args) {testOne (s);} private static void testOne (String s) {System.out.println (s); System.out.println (s.matches (". +\\ s.\\ S+ ")); / / Test". " Match any character and whitespace character "\ s and non-whitespace character\ S" System.out.println (s.matches ("[Amurz] + [1-9] +\\ s [A-Z] +"); / / Test any matching System.out.println of [a-zA-Z0-9] (s.matches ("[^ 0-9] + [^ aMuz] + [^\ S] [^ aMaiz] +") / / Test ^ (non) System.out.println (s.matches ("[BV [ja]] + [123] +\\ s [A-Z&&1JAV] +"); / / Test and or System.out.println (s.matches ("\\ w {4}\ d {3}\\ s\\ w {4}")) / / Test\ w characters and\ d numeric System.out.println (s.matches ("\\ D {4}\ w {3}\ W\\ D {4}")); / / Test non-word characters\ W and non-numeric\ D}
It is quite clear in the comments, and the output is as follows:
Java123JAVAtruetruetruetruetruetrue, after reading the above, do you have any further understanding of the role of regular expressions in java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.