ReplaceAll usage in Java __java

Source: Internet
Author: User

Java ReplaceAll is often used, so here is a brief introduction to its usage, which can be used in regular,

Use ?! Can ignore case,

Brief example:

(? i) ABC indicates that ABC ignores case

A (? i) BC indicates that BC ignores case
A ((? i) b) C means that only B ignores case
You can also Pattern.compile (rexp,pattern.case_insensitive) that the whole ignores case


Like what:

public string Transform2htmltag (string refstring) {//pattern: [tagName style = Stylevalue]text content[/TagName]//rep Lace to: <tagname style=\ "stylevalue\" >text content</tagname>.://e.g Text[span; color:red;font-family: ' Arial ']123 Content[/span]//replace to:text<span style= "font-size:14px;color:red;
	font-family: ' Arial ' ">123 content</span> String retvalue =" ";
		if (null!= refstring &&!refstring.trim (). IsEmpty ()) {RetValue = Refstring.trim (); RetValue = Retvalue.replaceall ("\\[" ([a-za-z][a-za-z0-9]*) \\s*style\\s*=\\s* ([^\\[]*) \] "," <$1 style=\ "$2\" >
		");
		
		RetValue = Retvalue.replaceall ("\\[/\\s* ([a-za-z][a-za-z0-9]*) \\s*\\]", "</$1>"); About handle all XSS security reason, must put this to the end position RetValue = Retvalue.replaceall ("(? i) < (. *) HT 
		TPS ([^<]*) > "," <$1h-t-t-p-s$2> "); 
		RetValue = Retvalue.replaceall ("(? i) < (. *) HTTP ([^<]*) >", "<$1h-t-t-p$2>"); RetValue =Retvalue.replaceall ("(? i) < (. *) SSL2 ([^<]*) >", "<$1s-s-l-2$2>");
	RetValue = Retvalue.replaceall ("(? i) < (. *) SSL ([^<]*) >", "<$1s-s-l$2>");
return retvalue;
 }
For example, here are examples of RetValue = Retvalue.replaceall ("(? i) < (. *) HTTPS ([^<]*) >", "<$1h-t-t-p-s$2>"); In HTTPSIs case-insensitive, that is to say, Https,https,https,https,https and other combinations of uppercase and lowercase are matched to facilitate matching operations,

Another example:

Replace:[size = sizevaluepx]text[/Size] 
//to: 	  <span style= "FONT-SIZE:SIZEVALUEPX;" >text</span>
//attend:px is the size unit pixel
//eg.: [size = 16px] text000 [/size] Convert to: <span St Yle= "16PX;" >text000</span>
RetValue = Retvalue.replaceall ("(? i) \\[\\s*size\\s*=\\s* ([^\\[]*) \]", "<span Style=\ "font-size:$1;\" (">");
RetValue = Retvalue.replaceall ("(? i) \\[/\\s*size\\s*\\]", "</span>");
Size can ignore case, or match different combinations such as size,size,size,size, which can be replaced by this regular match, and then converted to HTML format.


Brief introduction, remember to write, welcome to Pat Bricks ...






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.