1, simple match
In Java, strings can be used directly
String.matches (Regex)
Note: The regular expression matches all of the strings
2, Match and find
Find the substring that matches the regular expression in the string, Matcher the value in the angle bracket with the following instance
String str = "ABCDEF<LSZ>EFG";
String cmd = "<[^\\s]*>";
Pattern p = pattern.compile (cmd);
Matcher m = p.matcher (str);
if (M.find ()) {
System.out.println (M.group ());
}else{
System.out.println ("not Found");
}
At this point you can also find a matching number of groupings, you need to add in the regular expression brackets, a bracket corresponding to a group
String str= "XINGMING:LSZ,XINGBIE:NV";
String cmd= "xingming: ([a-za-z]*), Xingbie: ([a-za-z]*)" "
Pattern p = pattern.compile (cmd);
Matcher m = p.matcher (str);
if (M.find ()) {
System.out.println ("Name:" +m.group (1));
System.out.println ("Sex:" +m.group (2));
}else{
System.out.println ("not Found");
}
3, find and replace, the use of placeholders
String str= "Abcaabadwewewe";
String str2 = Str.replaceall ("([A])" [a]|[ D]) "," *$2 ")
The
str2 is: Abc*ab*dwewewe replaces A or D before a with *,$ as a placeholder in a regular expression.