regex
正規表現
文字列置換
ユニコード文字表現 \\uXXXX
を HTML実体参照 &#xXXXX
に変換
Perl
$str = "\\u306C\\u308B\\u307D&" $str =~ s/\\u([0-9a-fA-F]{4})/&#x$1;/g; print $str;
Ruby
str = "\\u306C\\u308B\\u307D" str.gsub!(/\\u([0-9a-fA-F]{4})/) { "&#x" + $1 + ";" } print str
subメソッドが文字列置換、gsubメソッドは、perlで言うgオプション付きの置換。!付きのメソッド(sub!、gsub!)はレシーバ自体を変更するので注意。
PHP
$str = "\\u306C\\u308B\\u307D" $str = preg_replace("/\\\\u([0-9a-fA-F]{4})/", "&#x$1;", $str); echo $str;
JavaScript
var str = "\\u306C\\u308B\\u307D"; str = str.replace(/\\u([0-9a-fA-F]{4})/g, function(m, p1, o, s){return "&#x" + p1 + ";"} ); document.write(str);
Java
String str = "\\u306C\\u308B\\u307D"; Matcher m = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str); StringBuffer buf = new StringBuffer(); while (m.find()) { m.appendReplacement(buf, "&#x$1;"); } m.appendTail(buf); System.out.println(buf);
String#replaceAllメソッドは後方参照可能。replaceメソッドは不可。
String str = "\\u306C\\u308B\\u307D"; str = str.replaceAll("\\\\u([0-9a-fA-F]{4})", "&#x$1;"); System.out.println(str);
C#.NET
string str = "\\u306C\\u308B\\u307D"; Regex regex = new Regex("\\u([0-9a-fA-F]{4})"); str = regex.Replace(str, "&#x$1;"); Console.WriteLine(str);
regex.txt · 最終更新: 2008/01/19 12:13 by 127.0.0.1