Example of Java Regular Expression extraction and replacement

Source: Internet
Author: User
View code

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 indicates Chinese characters, and \ uff00-\ Uffff indicates all other characters.
Public static string regexstr = "\ {([\ u4e00-\ u9fa5A-Za-z0-9 \\'\",;.:?! , "<> ',;. :?! <>:\ \ S \ uff00-\ 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 = 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 = 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 body: {weight 1 :}23 :{:} SAA {d China ks1} asdadsa {dk2} Asda {dkasd1233s} sad2rty34 ";
System. Out. println (replacedefaultkey (teststr, "kkkkkk ",
Ideacontenttype. Title ));

}

}

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();

}

------------------------------------------------------

You must provide a method to replace linefeeds in strings.

For Linux and Unix systems, the line feed is "\ n", while for Windows systems, the line feed is not "\ n" or "\ r \ n ". So out. Write

("\ N") only one black box can be obtained, because S does not consider this as a "line feed ". If you enter it directly from notepad, Windows will automatically

"\ R \ n" is input, so "\ r \ n" is also read from the text file, which can be displayed normally.

If the regular expression is written as ^ ([^ \ n] * (\ n) * $
All matched content is highlighted. For example
ABC
ABC
ABC
If the regular expression is written as [\ n-\ r], only the line break between the content is matched.

ABC

ABC

Line breaks are highlighted invisible.

Replacement method:

    public static String replaceIllegalCharacter(String source) {
if (source == null)
return source;
String reg = "[\n-\r]";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(source);
return m.replaceAll("");
}

-------------------------------------------------------

Note:

Many times, Regular Expression Processing is performed on the client, but some also need to be processed at the backend, such as uploading an Excel file and reading a row to record. for row validation, cleaning may need to be processed at the backend.

Appendix:

Http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html

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.