Created
November 2, 2013 04:33
-
-
Save alucky0707/7275546 to your computer and use it in GitHub Desktop.
JavaでJavaScriptのように正規表現で置換する ref: http://qiita.com/alucky0707/items/ab1babc0bcde9f3e4ef6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var | |
| str = 'Trick or Treat', | |
| rep = str.replace(/[a-zA-Z]/g, function (c, i, str) { | |
| console.log("%s:%d:%s", c, i, str); | |
| return 'a' <= c && c <= 'z' && c.toUpperCase() || | |
| 'A' <= c && c <= 'Z' && c.toLowerCase(); | |
| }); | |
| console.log(rep); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| T:0:Trick or Treat | |
| r:1:Trick or Treat | |
| i:2:Trick or Treat | |
| c:3:Trick or Treat | |
| k:4:Trick or Treat | |
| o:6:Trick or Treat | |
| r:7:Trick or Treat | |
| T:9:Trick or Treat | |
| r:10:Trick or Treat | |
| e:11:Trick or Treat | |
| a:12:Trick or Treat | |
| t:13:Trick or Treat | |
| tRICK OR tREAT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| import java.util.regex.MatchResult; | |
| import java.util.regex.Pattern; | |
| public class RegexSample { | |
| static public void main(String... args) { | |
| String str = "Trick or Treat"; | |
| Pattern pat = Pattern.compile("[a-zA-Z]"); | |
| String rep = Replacer.replace(str, pat, new ReplaceCallback() { | |
| @Override | |
| public String replaceString(String matched, List<String> groups, | |
| String str, Pattern pat, MatchResult matchResult) { | |
| System.out.printf("%s:%d:%s\n", matched, matchResult.start(), str); | |
| char c = matched.charAt(0); | |
| c = (char)('a' <= c && c <= 'z' ? c - 0x20 : | |
| 'A' <= c && c <= 'Z' ? c + 0x20 : c); | |
| return Character.toString(c); | |
| } | |
| }); | |
| System.out.println(rep); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class RegexSample { | |
| static public void main(String... args) { | |
| String str = "Trick or Treat"; | |
| Pattern pat = Pattern.compile("[a-zA-Z]"); | |
| Matcher m = pat.matcher(str); | |
| StringBuilder sb = new StringBuilder(); | |
| int start = 0; | |
| while(m.find()) { | |
| System.out.printf("%s:%d:%s\n", m.group(), m.start(), str); | |
| sb.append(str.substring(start, m.start())); | |
| start = m.end(); | |
| char c = m.group().charAt(0); | |
| if ('a' <= c && c <= 'z') sb.append((char)(c - 0x20)); | |
| else if ('A' <= c && c <= 'Z') sb.append((char)(c + 0x20)); | |
| } | |
| sb.append(str.substring(start)); | |
| System.out.println(sb.toString()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| import java.util.regex.MatchResult; | |
| import java.util.regex.Pattern; | |
| public interface ReplaceCallback { | |
| String replaceString(String matched, List<String> groups, String str, Pattern pat, MatchResult matchResult); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class Replacer { | |
| static String replace(String str, Pattern pat, ReplaceCallback callback) { | |
| StringBuilder sb = new StringBuilder(); | |
| Matcher m = pat.matcher(str); | |
| int start = 0; | |
| while (m.find()) { | |
| sb.append(str.substring(start, m.start())); | |
| String matched = str.substring(m.start(), m.end()); | |
| start = m.end(); | |
| ArrayList<String> groups = new ArrayList<>(); | |
| for (int j = 0; j <= m.groupCount(); j++) { | |
| groups.add(m.group(j)); | |
| } | |
| sb.append(callback.replaceString(matched, groups, str, pat, m.toMatchResult())); | |
| } | |
| sb.append(str.substring(start)); | |
| return sb.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment