Regular expression defines the pattern string.
Regular expressions can be used to search, edit, or processing text.
Regular expression is not limited to a particular language, but there are subtle differences in each language.
Examples of regular expressions
A string is actually a simple regular expressions, such as Hello World regular expression matches "Hello World" string.
. (Dot) is a regular expression, it matches any character, such as: "a" or "1".
Here are some examples and describe regular expressions:
Java regular expressions and Perl is the most similar.
java.util.regex packet mainly includes the following three categories:
- Pattern categories:
Object pattern is a regular expression compiler representation. Pattern no public class constructor. To create a Pattern object, you must first call its public static compile method that returns a Pattern object. The method accepts a regular expression as its first parameter.
- Matcher categories:
Matcher object is input string matching operation will be explained and the engine. Like the Pattern class, Matcher no public constructor. You need to call the matcher method Pattern object to obtain a Matcher object.
- PatternSyntaxException:
PatternSyntaxException is a non-mandatory exception class that represents a syntax error in the regular expression pattern.
Import the java.util.regex *. ; class RegexExample1 { public static void main (String args []) { String Content = "from the I AM noob runoob.com." ; String pattern =. "runoob * *." ; Boolean IsMatch = Pattern.matches (pattern, Content); System.out.println ( "? contains the string 'runoob' substring" + IsMatch); } }
operation result:
Capture group
Capturing a plurality of characters when the group is a single method of processing unit, which is created by grouping the characters in the brackets.
For example, the regular expression (Dog) creates a single packet group contains "d", "o", and "g".
Capturing groups are numbered by counting left to right from its open parenthesis. For example, in the expression ((A) (B (C))), there are four such groups:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
Can be viewed by groupCount method calls matcher object expression how many packets. groupCount method returns an int value representing a plurality of objects currently matcher capture group.
There is also a special group (group (0)), it always represents the entire expression. The group is not included in the return value groupCount.
Examples
The following example shows how to find a string of digits from the given string:
Import java.util.regex.Matcher; Import java.util.regex.Pattern; public class var { public static void main (String [] as AGRS) { // the specified pattern search string String line = "This order was placed ! for QT3000 the OK? " ; String pattern =" (\\ d *) (\\ d +) "(*.) ; // create pattern objects pattern r = Pattern.compile (pattern); // now create an object matcher Matcher = m r.matcher (Line); IF (m.find ()) { System.out.println ( "Found value:" + m.group (0 )); System.out.println("Found value: " + m.group(1) ); System.out.println("Found value: " + m.group(2) ); System.out.println("Found value: " + m.group(3) ); } else { System.out.println("NO MATCH"); } } }
operation result:
Regular expression syntax
In Java, \\ said: I want to insert a backslash a regular expression, so the following character has special meaning.
So, in other languages (such as Perl), a backslash \ is enough to have escaped role, and in Java regular expression in two backslash you need to have in order to be resolved to turn in other languages righteous action. Also can be simply understood in regular expressions Java, two \\ on behalf of one of the other languages \, which is why a digital representation of the regular expression is \\ d, which represents one ordinary backslash It is \\\\.
Matcher class method
case analysis:
Import java.util.regex.Matcher; Import java.util.regex.Pattern; public class var { Private static Final String REGEX = "\\ \\ BCAT B" ; // set rules Private static Final String the INPUT = "CAT CAT Cattie CAT CAT " ; // needs to be analyzed and matched string public static void main (string [] as AGRS) { the Pattern P = of Pattern.compile (REGEX); Matcher m = p.matcher (the INPUT); // Get matcher Object int COUNT = 0 ; the while (m.find ()) {// cycle according to the rules found in the string COUNT ++ ; Of System.out.print ( "Start ():" + m.start ()); // start position of the matched string System.out.println ( "; End ():" + m.end ()); // end position of the matched string } System.out.println ( "Number match" + COUNT); } }
operation result:
Case 2:
Import java.util.regex.Matcher; Import java.util.regex.Pattern; / ** * lookingAt method requires matching starts from the first character, to match returns TRUE. After the match behind whether there do not care about the characters. * Matches require a complete match, matching string is not the presence of extra characters that can not be matched, it returns TRUE. * / Public class var { Private static Final String REGEX = "foo" ; Private static Final String the INPUT = "fooooooooooooooooo" ; Private static Final String INPUT2 = "ooooofoooooooooooo" ; Private static the Pattern pattern; Private static Matcher Matcher; private static Matcher matcher2; public static void main(String[] agrs){ pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); matcher2 = pattern.matcher(INPUT2); System.out.println("Current REGEX is: "+REGEX); System.out.println("Current INPUT is: "+INPUT); System.out.println("Current INPUT2 is: "+INPUT2); System.out.println("lookingAt(): "+matcher.lookingAt()); System.out.println("matches(): "+matcher.matches()); System.out.println("lookingAt(): "+matcher2.lookingAt()); } }
operation result:
Case 3:
/ ** * the replaceAll: complies with rules to a string for string replacement. * ReplaceFirst: replace the first line with the rules. * / Public class var { Private static String REGEX = "Dog" ; Private static String the INPUT = "of The Dog (Pic) Meow All Dogs say Meow.." ; Private static String the REPLACE = "CAT" ; public static void main (String [] as AGRS) { the Pattern P = of Pattern.compile (REGEX); Matcher m = p.matcher (the INPUT); the INPUT = m.replaceFirst (the REPLACE); System.out.println (the INPUT); the INPUT= m.replaceAll(REPLACE); System.out.println(INPUT); } }
operation result:
Case 4:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class var { private static String REGEX = "a*b"; private static String INPUT = "aabfooaabfooabfoobkkk"; private static String REPLACE = "-"; public static void main(String[] agrs){ Pattern p = Pattern.compile(REGEX); // 获取 matcher 对象 Matcher m = p.matcher(INPUT); StringBuffer sb = new StringBuffer(); the while (m.find ()) { m.appendReplacement (SB, the REPLACE); // replace string INPUT, and writes the new string buffer SB } m.appendTail (SB); // remaining string spliced into sb in. System.out.println (sb.toString ()); } }
operation result:
Exercise:
Matching regular mail to write.
Import java.util.regex.Matcher; Import java.util.regex.Pattern; public class var { / ** * [0-9] {6,10}: (0-9 data matches 6-10) * @ matches a symbol @ * [a-Za-z0-9] {2,4} matching letters, numbers match 2-4 times. * (.Com.cn)? Matches zero or 1 times the string * / Private static String REGEX = "[0-9] {6,10} @ [A-Za-Z0-9] {2,4} (. Com.cn) (COM.)?? " ; Private static String the INPUT =" [email protected] ;; hjajshflsdfl; er29953345563344533788988 @ qq.com.cnhasjkercssdffssd @ 163.com.cn " ; Private static String = REPLACE" - " ; public static void main(String[] agrs){ Pattern p = Pattern.compile(REGEX); // 获取 matcher 对象 Matcher m = p.matcher(INPUT); while(m.find()){ System.out.println(INPUT.substring(m.start(),m.end())); } } }
operation result:
Reference: https://www.runoob.com/java/java-regular-expressions.html