In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use regular expressions to deal with text data in Java", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use regular expressions to deal with text data in Java" this article.
A regular expression is a string, but unlike an ordinary string, a regular expression is an abstraction of a similar set of strings, such as the following strings:
A98b c0912d c10b a12345678d ab
When we carefully analyze the above five strings, we can see that they have a common feature, that is, the first character must be'a'or 'croup, the last character must be' b'or 'dash, and the middle character is composed of any number of digits (including 0 digits). Therefore, we can abstract the common features of these five strings, which results in a regular expression: [ac]\\ d * [bd]. According to this regular expression, we can write an infinite number of strings that meet the conditions.
There are many ways to use regular expressions in Java, and the easiest is to use them with strings. There are four methods in String that can use regular expressions: matches, split, replaceAll, and replaceFirst.
1. Matches method
The matches method determines whether the current string matches a given regular expression. True is returned if there is a match, otherwise false is returned. The matches method is defined as follows:
Public boolean matches (String regex)
Such as the regular expression given above, we can verify it with the following program.
String [] ss = new String [] {"a98b", "c0912d", "c10b", "a12345678d", "ab"}; for (String s: ss) System.out.println (s.matches ("[ac]\\ d * [BD]"))
Output result:
True
True
True
True
True
Let's briefly explain the meaning of this regular expression. If we have learned the lexical analysis of compilation principles, it will be easy to understand the above regular expressions (because the representation of regular expressions is similar to the expressions in lexical analysis). As in [...] Equivalent to or "|" in, for example, [abcd] equals a | b | c | d, that is, an or b or c or d. If the above regular expression begins with [ac], it means that the string can only begin with an or c. [bd] the expression string can only end with b or d. The middle\ d expresses the number 0-9, and because\ has a special meaning in the regular expression, it is represented by\. And * means there are 0 or infinite numbers (this is called * closure in lexical analysis), and because * follows\ d, there are 0 or infinite numbers.
2. Split method
The split method uses regular expressions to split the string and returns the result as an array of String. There are two overloaded forms of split, which are defined as follows:
Public String [] split (String regex) public String [] split (String regex, int limit)
As in the following code, the first line of the HTTP request header is split using the first overloaded form of split, as follows:
String s = "GET / index.html HTTP/1.1"; String ss [] = s.split ("+"); for (String str: ss) System.out.println (str)
Output result:
GET
/ index.html
HTTP/1.1
When using the first overloaded form of split, it should be noted that if the split string ends with an empty string, it will be ignored. If you use the regular expression\ d to split the string a0b1c3456, the resulting array has a length of 3 instead of 7.
In the second overloaded form of split, there is a limit parameter, which is discussed in three cases:
1. Greater than 0: if the value of limit is n, then the regular expression will be used once, as shown in the following code:
String s = "a0b1c3456"; String ss [] = s.split ("\\ d", 3); for (String str: ss) System.out.println (str)
Output result:
A
B
C3456
As can be seen from the output, the program only uses the regular expression for "a0b1c3456" twice, that is, after scanning the character'1' less, regardless of whether there is a string that satisfies the condition or not, it takes the following string as a whole as the last value of the return array.
two。 Less than 0: the empty string at the end is not ignored. That is, the above example returns that the length of the array should be 7, not 3.
3. Equal to 0: this is the default value, equivalent to the first overloaded form of split.
3. ReplaceAll and replaceFirst methods
The definitions for the two methods are as follows:
Public String replaceAll (String regex, String replacement) public String replaceFirst (String regex, String replacement)
These two methods replace the string in the current string that matches regex with replacement.
These are all the contents of the article "how to use regular expressions to deal with text data in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.