[hello world] のような文字列があり、hello world を取得したい
JavaのreplaceAllメソッドの正しい正規表現を見つけるのに苦労しています
文字列 s = s.replaceAll("[[]]",""); 例外をスローします
それに適した正規表現は何ですか?
[
およびは、文字クラス]
の境界を定義するために使用されるメタ文字です。正規表現自体で使用するには、エスケープする必要があります
s = s.replaceAll("[\\[\\]]","");
おそらく、replace
代わりに使用してみてください:
String s = string.replace("[", "").replace("]", "");
または、部分文字列を使用して逃げることができるようです:
String s = string.substring(1, string.length()-1);
これを試すことができます:-
String s = string.replace("[", "").replace("]", "");
これを試して
public class Test {
public static void main(String args[]){
String s = "[hello world]";
s= s.replaceAll("\\[", "").replaceAll("\\]", "");
System.out.println(s);
}
}