Replacement of Java regular expressions with multi-String Matching

Source: Internet
Author: User

Java is also relatively simple to use:
1. Compile the literal value of the regular expression to obtain the Pattern object;

2. Create a Matcher that matches the given input in this mode;

3. perform operations through the matching object. The methods of the matching object are rich, and the combination of methods is more powerful.

Copy codeThe Code is as follows: public static void main (String [] args ){
// Data source with the replaced keyword
Map <String, String> tokens = new HashMap <String, String> ();
Tokens. put ("cat", "Garfield ");
Tokens. put ("beverage", "coffee ");

// Match a string similar to a velocity rule
String template = "$ {cat} really needs some $ {beverage }.";
// Generate a regular expression for the matching mode
String patternString = "\\\ \\{ (" + StringUtils. join (tokens. keySet (), "|") + ")\\}";

Pattern pattern = Pattern. compile (patternString );
Matcher matcher = pattern. matcher (template );

// Two methods: appendReplacement and appendTail
StringBuffer sb = new StringBuffer ();
While (matcher. find ()){
Matcher. appendReplacement (sb, tokens. get (matcher. group (1 )));
}
Matcher. appendTail (sb );

// Out: Garfield really needs some coffee.
System. out. println (sb. toString ());

// For special characters "\", "$", use Matcher. quoteReplacement to eliminate special meanings
Matcher. reset ();
// Out: cat really needs some beverage.
System. out. println (matcher. replaceAll ("$1 "));
// Out: $1 really needs some $1.
System. out. println (matcher. replaceAll (Matcher. quoteReplacement ("$1 ")));

// Obtain the prefix of the email address. In fact, there are a variety of regular expressions in the verification email address. It is king to write the corresponding regular expressions according to your own needs.
String emailPattern = "^ ([a-z0-9 _\\. \-\ +] +) @ ([\ da-z \\. \-] + )\\. ([a-z \.] {2, 6}) $ ";
Pattern = Pattern. compile (emailPattern );
Matcher = pattern. matcher ("test@qq.com ");
// Verify whether the email address is used
System. out. println (matcher. find ());
// Obtain the mailbox name out: test before the @ symbol.
System. out. println (matcher. replaceAll ("$1 "));

// Obtain the matching Value
String temp = "<meta-data android: name = \" appid \ "android: value = \" joy \ "> </meta-data> ";
Pattern = Pattern. compile ("android :( name | value) = \" (. + ?) \"");
Matcher = pattern. matcher (temp );
While (matcher. find ()){
// Out: appid, joy
System. out. println (matcher. group (2 ));
}
}

Some people forget the basics

[...] Any character in parentheses

[^...] Any character not in parentheses

Any character except line breaks is equivalent to [^ \ n]

\ W any single character, equivalent to [a-zA-Z0-9]

\ W any non-single character, equivalent to [^ a-zA-Z0-9]

\ S any blank space character, equivalent to [\ t \ n \ r \ f \ v]

\ S any non-blank character, equivalent to [^ \ t \ n \ r \ f \ v]

\ D any number, equivalent to [0-9]

\ D any character except number, equivalent to [^ 0-9]

[\ B] A return direct quantity (Special Case)

{N, m} matches the first item at least n times, but cannot exceed m times

{N,} matches the previous item n times or multiple times.

{N} matches the first item EXACTLY n times.

? Match the first item 0 or 1, that is, the first item is optional. equivalent to {0, 1}

+ Match the previous item once or multiple times, equivalent to {1 ,}

* Match the first item 0 or multiple times. It is equivalent to {0 ,}

| Select. match either the child expression on the left of the symbol or the child expression on the right of the symbol.

(...) Grouping. Several projects are divided into one Unit. This unit can be divided by *, + ,? And |. You can also remember the characters that match this group for future reference.

\ N matches the characters matching the nth group. The group is a subexpression (which may be nested) in the brackets. The group number is the number of left parentheses counted from left to right.

^ Match the beginning of a character. In multi-row search, match the beginning of a line

$ Matches the end of a character. In multi-row search, it matches the end of a row.

\ B matches the boundary of a word. In short, it is located between the characters \ w and \ w (Note: [\ B] matches the return character)

\ B matches non-word boundary characters

Digress

Email verification: previously verified email addresses were used to search for regular expressions on the Internet and install them in your own programs. In fact, this is not correct. Different companies have different email verification formats, for example, the formats required for registration in 163 and QQ mail are different. Therefore, it is incorrect to search for a regular expression and set all email formats. Regular expressions that meet your needs are correct.

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.