Java Regular multiple string matching replace _java

Source: Internet
Author: User
Tags stringbuffer

The use of Java is also relatively simple:
1. Compiling regular expressions literal is worthwhile to the corresponding pattern patterns object;

2. Create matching device matcher matching given input with this pattern;

3. The method of matching objects is rich, and the combination of methods is more powerful with the action of the matching object.

Copy Code code as follows:

public static void Main (string[] args) {
The data source for the replaced keyword
Map<string,string> tokens = new hashmap<string,string> ();
Tokens.put ("Cat", "Garfield");
Tokens.put ("beverage", "coffee");

Matches a string similar to the velocity rule
String template = "${cat} really needs some ${beverage}.";
A regular expression that generates a matching pattern
String patternstring = "\\$\\{(" + Stringutils.join (Tokens.keyset (), "|") + ")\\}";

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

Two methods: Appendreplacement, 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 meaning characters "\", "$", use matcher.quotereplacement to eliminate special meaning
Matcher.reset ();
Out:cat really needs some beverage.
System.out.println (Matcher.replaceall ("$"));
Out: $ really needs some $.
System.out.println (Matcher.replaceall) (Matcher.quotereplacement ("$"));

The prefix name of the mailbox to be. Insert a sentence, in fact, verify the mailbox is a variety of, according to their own needs to write the corresponding is the King
String Emailpattern = "^ ([a-z0-9_\\.\\-\\+]+) @ ([\\da-z\\.\\-]+) \ \. ([a-z\\.] {2,6}) $";
Pattern = Pattern.compile (Emailpattern);
Matcher = Pattern.matcher ("test@qq.com");
Verify whether the mailbox
System.out.println (Matcher.find ());
Get the mailbox name before @ symbol Out:test
System.out.println (Matcher.replaceall ("$"));

Get matching value
The 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 always forget the basics


[...] Any character that is within the parentheses

[^...] Any character not in parentheses

. Any character other than a line break, equivalent to [^\n]

\w any single word character, equivalent to [a-za-z0-9]

\w any non-word character, equivalent to [^a-za-z0-9]

\s any whitespace, equivalent to [\ t \ n \ r \ f \ V]

\s any non-white-space character, equivalent to [^\ t \ n \ r \ f \ V]

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

\d any character other than a number, equivalent to [^0-9]

[\b] A backspace direct amount (special case)

{n, m} matches the previous item at least n times, but not more than m times

{n,} matches n times before, or multiple

{n} matches the previous item exactly n times

? Matches the previous item 0 or 1 times, which means the previous item is optional. Equivalent to {0, 1}

+ matches 1 or more times before, equivalent to {1,}

* Match the previous item 0 or more times. Equivalent to {0,}

| Select the. Match either the subexpression to the left of the symbol, or the subexpression on its right side

(...) Group. Divides several items into one unit. This unit can be made up of *, +,? and | symbols, and you can also remember characters that match this group for later reference use

\ n matches the characters that are matched by the nth group. The grouping is a subexpression (possibly nested) in parentheses. The group number is a left to right count of the number of left parentheses

^ matches the beginning of a character, and in multiple-line retrieval, it matches the beginning of a line

The $ match is the end of the character, and in multiple-row retrieval, the match is the end of a line

\b matches the boundary of a word. In short, the position between the character \w and the \w (note: [\b] matches backspace)

\b A character that matches the bounds of a non-word


Digression


E-mail verification, previously verified mailbox, online search is installed in their own program inside the use, in fact, this is wrong, different companies to verify the format of the mailbox is not the same, for example, 163 and QQ mailbox registration, they require a different format, so search a regular expression to go to set all the mailbox format is also wrong, It is right to meet one's own needs.

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.