How does ReplaceAll ignore capitalization in Java?
Method One: Add in front of the regular expression (? i)
Java code
- @Test
- public void test_replaceall33 () {
- string input = "I like java,java is very easy and java is so popular. ";
- string replacement=
-
-  SYSTEM.OUT.PRINTLN (input);
- System.out.println (Input.replaceall (
- }
@Testpublic void Test_replaceall33 () {String input = "I like Java,java are very easy and Java are so popular."; String replacement= "CCCC"; SYSTEM.OUT.PRINTLN (input); System.out.println (Input.replaceall ("(? i) Java", replacement));}
Test results:
Mode two: Using the Appendreplacement method of Matcher
Java code
- @Test
- public void Test_replaceall () {
- String input = "I like Java,java are very easy and Java are so popular.";
- String regex = "Java";
- String replacement="CCCC";
- StringBuffer SB =replaceall2 (input, regex, replacement);
- SYSTEM.OUT.PRINTLN (input);
- System.out.println (SB);
- }
- /***
- * ReplaceAll, ignoring case
- * @param input
- * @param regex
- * @param replacement
- * @return
- */
- Public StringBuffer replaceAll2 (String input,string regex,string replacement) {
- Pattern p = pattern.compile (regex,pattern.case_insensitive);
- Matcher m = p.matcher (input);
- StringBuffer sb = new StringBuffer ();
- Boolean result = M.find ();
- While (result)
- {
- M.appendreplacement (SB, replacement);
- result = M.find ();
- }
- M.appendtail (SB);
- return SB;
- }
@Testpublic void Test_replaceall () {String input = "I like Java,java are very easy and Java are so popular."; String regex = "Java"; String replacement= "CCCC"; StringBuffer SB =replaceall2 (input, regex, replacement); SYSTEM.OUT.PRINTLN (input); System.out.println (SB);} /*** * ReplaceAll, ignoring case * @param input * @param regex * @param replacement * @return */public stringbuffer replaceAll2 (Str ing input,string regex,string replacement) {Pattern p = pattern.compile (regex,pattern.case_insensitive); Matcher m = p.matcher (input); StringBuffer sb = new StringBuffer (); Boolean result = M.find (); while (result) { m.appendreplacement (SB, replacement); result = M.find (); } M.appendtail (SB); return SB;}
Test results:
Method Three: Use Matcher's ReplaceAll
Java code
- /***
- * ReplaceAll, ignoring case
- *
- * @param input
- * @param regex
- * @param replacement
- * @return
- */
- Public string ReplaceAll3 (String input, string regex, string replacement) {
- Pattern p = pattern.compile (regex, pattern.case_insensitive);
- Matcher m = p.matcher (input);
- String result = M.replaceall (replacement);
- return result;
- }
- @Test
- public void Test_replaceall3 () {
- String input = "I like Java,java are very easy and Java are so popular.";
- String regex = "Java";
- String replacement = "CCCC";
- String sb = ReplaceAll3 (input, regex, replacement);
- SYSTEM.OUT.PRINTLN (input);
- System.out.println (SB);
- }
/*** * ReplaceAll, ignoring case * * @param input * @param regex * @param replacement * @return */public String replaceAll3 (Str ing input, string regex, string replacement) {Pattern p = pattern.compile (regex, pattern.case_insensitive); Matcher m = p.matcher (input); String result = M.replaceall (replacement); return result;} @Testpublic void Test_replaceall3 () {String input = "I like Java,java are very easy and Java are so popular."; String regex = "Java"; String replacement = "CCCC"; String sb = ReplaceAll3 (input, regex, replacement); SYSTEM.OUT.PRINTLN (input); System.out.println (SB);}
Test results: Reference: http://hw1287789687.iteye.com/blog/2150892
Java ReplaceAll is case insensitive