Regular expression Usage Summary _ regular expression

Source: Internet
Author: User
Tags regular expression
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.