Jsp regular expression tutorial

Source: Internet
Author: User
Tags character classes character set stringbuffer

Package regex;

Import java. io. IOException;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;

/**
* Note: Matcher is the main operation class of the regular expression. It contains the most important method for extraction and replacement. Pattern is not the main class.
* ReplaceAll is used to replace all. replaceFirst or replaceEnd can be used for recursive replacement.
*
* @ Author gaoyibo
*
*/
Public class RegexAPITest {

Public static final int TITLE_LEN = 50;
Public static final int DESC_LEN = 80;
// U4e00-u9fa5 represents Chinese, uFF00-uFFFF represents all other represents some special characters.
Public static String regexStr = "{([u4e00-u9fa5A-Za-z0-9 '",;.:?! , <> ',;. :?! <>: SuFF00-uFFFF] *)} ";

Public static enum IdeaContentType {
TITLE, DESC1, DESC2, ACCESS_URL, SHOW_URL
    }

/**
* Requirement description: Search for ads and audit logs in admin. The default keyword must be replaced during industry audits. The rules for replacement are as follows:
* 1. For title, if the length after replacement exceeds 50, for desc1 and desc2, if the length after replacement exceeds 80, it is not replaced. The default keyword is used and displayed as a green font;
* 2. If it is not exceeded, use the specified keyword to replace it with a red font.
     *
* @ Author gaoyibo
* @ Param source
* Title or desc1 or desc2 to be replaced
* @ Param key
* Specified keywords
* @ Param type
* @ Return
*/
Public static String replaceDefaultKey (String source, String key,
IdeaContentType type ){
String result = "";
If (source = null | source. length () <= 0)
Return result;
Matcher matcher = Pattern. compile (regexStr). matcher (source );
If (! Matcher. find ()){
Return source;
        }
String replaceFormatKey = "<font color = 'red'>" + key + "</font> ";
// Judge based on the length of all values after Replacement. If the value is too long, the default keywords in the source are not replaced by the [formatted key]. If the value is not too long, the value is replaced.
Result = Pattern. compile (regexStr). matcher (source). replaceAll (key );
Switch (type ){
Case TITLE:
// No Replacement. Use the default keyword, but the default keyword must be formatted.
If (result. length ()> TITLE_LEN ){
Return doReplace (source );
            }
// Replace
Return matcher. replaceAll (replaceFormatKey );
Case DESC1:
If (result. length ()> DESC_LEN ){
Return doReplace (source );
            }
Return matcher. replaceAll (replaceFormatKey );
Case DESC2:
If (result. length ()> DESC_LEN ){
Return doReplace (source );
            }
Return matcher. replaceAll (replaceFormatKey );
Default:
Return source;
        }

    }

/**
* Recursive method: format the first matched default keyword each time.
     *
* @ Author gaoyibo
* @ Param source
* @ Return
*/
Public static String doReplace (String source ){
Matcher matcher = Pattern. compile (regexStr). matcher (source );
While (matcher. find ()){
// Extract matching content.
String keytmp = matcher. group ();
String defaultFormatKey = "<font color = 'green'>"
+ Keytmp. substring (1, keytmp. length ()-1) + "</font> ";

// Replace the first matching content, replace it, and then recursion.
Return doReplace (matcher. replaceFirst (defaultFormatKey); // after formatting the current default keyword, the returned string is recursive until all the default keywords are formatted.
        }
Return source;
    }

Public static void main (String [] args) throws IOException {
// The following string contains wildcards of multiple default keywords, each of which is different.
String testStr = "asd: {weight 1 :}23 :{:} saA {d ks1} asdadsa {DK2} asda {dk} sad2rty34 ";
System. out. println (replaceDefaultKey (testStr, "kkkkkk ",
IdeaContentType. TITLE ));

    }

}

1. Which of the following public classes are included in the java. util. regex package? Describe their functions.
2. Consider the Start Index of the string "foo? What is the ending index? Explain the meaning of these numbers.
3. What are the differences between common characters and metacharacters? Each of them provides an example.
4. How can we make metacharacters look like normal characters?
5. What is the character set with square brackets? What does it do?
6. Here are three predefined character classes: d, s, and w. What are their meanings? And use square brackets to rewrite them.
7. Write two simple expressions for d, s, and w to match their opposite character sets.
8. Think about the regular expression (dog) {3} and identify the two subexpressions. What string will this expression match?

Exercise 〗
1. Use reverse references to write an expression that matches a person's name. Assume that the first name of the person is the same as the last name.

[Answer]
1. Q: Which three common classes are included in the java. util. regex package? Describe their functions.

A:
The compiled Pattern instance indicates a regular expression.
The Matcher instance is an engine for parsing mode and matching by input strings.
PatternSyntaxException defines an unchecked exception that indicates a syntax error in the regular expression.
2. Q: What is the start index of the string "foo? What is the ending index? Explain the meaning of these numbers.

A: each character in a string is located in its own cell. The index is located between two cells. The string "foo" starts with index 0 and ends with index 3, even if these characters only occupy cells 0, 1, and 2.
3. Q: What are the differences between common characters and metacharacters? Each of them provides an example.
A: regular characters in regular expressions match themselves. Metacharacters are special characters that affect the pattern to be matched. The letter A is A common character. Punctuation. It is a metacharacter that matches any single character.
4. Q: How do I imaging metacharacters like normal characters?
A: There are two methods:
Add a backslash () before the metacharacter ();
Place metacharacters in the reference expression of Q (start) E (end.
5. Q: What is the character set with square brackets? What does it do?
A: It is a character class. Matches any character in the specified character class using an expression between square brackets.
6. Q: Here are three predefined character classes: d, s, and w. What are their meanings? And use square brackets to rewrite them.
A: d matches any number [0-9].
S match any blank character [tn-x0Bfr]
W match any word character [a-zA-Z_0-9]
7. Q: write two simple expressions for d, s, and w to match their opposite character sets.
A: d D [^ d]
S S [^ s]
W W [^ w]
8. Q: think about the regular expression (dog) {3} and identify the two subexpressions. What string will this expression match?
A: The expression is composed of a capture group (dog) and a greedy quantizer {3. It matches the string "dogdogdog ".
[Exercise answer]
1. Exercise: use reverse references to write an expression that matches a person's name. Assume that the first name of the person is the same as the last name.
Answer: [A-Z] [a-zA-Z] *) s1
Comment return Directory [1] The full text of this article is translated from the Regular Expressions of Java Tutorial. The title is self-prepared by the translator. -- Translator's note

[2] Unix tool, used for searching strings in files. It is one of the earliest regular expression tools. -- Translator's note

[3] to exit, use Ctrl + C to interrupt. -- Translator's note

[4] "index 3" in the figure indicates that it is added by the translator, but not in the original text. -- Translator's note

[5] When using this method in versions earlier than JDK 6.0, it should be noted that using this structure in character classes is a bug, but it has been fixed in JDK 6.0. -- Translator's note

[6] If no Q exists before E, a PatternSyntaxException is generated, indicating a syntax error. -- Translator's note

[7] The first match matches only the start part of the string, similar to. (Taken from Jeffrey E. F. Friedl, Mastering Regular Expressions, 3rd ed., & sect; 3.5.3.3, O 'Reilly, 2006.)

[8] u030A, that is, the small circle in the upper part of the character & aring; (this character cannot be correctly displayed on the IE browser and can be normally displayed on the Firefox browser ). -- Translator's note

[9] The method added in JDK 5.0 is unavailable in JDK 1.4. -- Translator's note

[10] applicable versions of JDK 1.4 and JDK 5.0 are included in the source code. For JDK 1.4, the file name is RegexTestHarness2V4. java, and for JDK 1.5, the file name is RegexTestHarness2V5. java. -- Translator's note

[11] The third edition is the latest version of this book. The Chinese translation of the third edition, proficient regular expressions, was published by the electronics industry Publishing House in July 2007. -- Translator's note

 

 

It is found that the combination of appendReplacement and appendTail can also play the role of the above recursive method. For example:
Public static String doReplace2 (String source) {Matcher matcher = Pattern. compile (regexStr ). matcher (source); StringBuffer sb = new StringBuffer (); while (matcher. find () {String keytmp = matcher. group (); String defaultFormatKey = "<font color = 'green'>" + keytmp. substring (1, keytmp. length ()-1) + "</font>"; matcher. appendReplacement (sb, defaultFormatKey);} matcher. appendTail (sb); return sb. toString ();}

Related Article

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.