What would be a concise regular regular expression to remove everything from a string that is not an alphabet
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=0;
try{
T= Integer.parseInt(br.readLine());
while(T>0)
String input=br.readLine();
**String res= input.replaceAll("^[a-zA-Z]"," " );**
System.out.println(res);
Also tried
input.replaceAll("[^a-zA-Z]]"," " )
Neither of them is replacing anything from the input string.The input string remains just as it was
Edit:
input.replaceAll("[^a-zA-Z]"," " ) //works well
input.replaceAll("^[a-zA-Z]"," " ) //replaces first char of string
You need:
You need to make the
inputreference point to the String object returned by thereplaceAllmethod to get the changed string as Strings are immutable.The range
A-zis not something you want. You needA-Z.Also you have an extra
]in your pattern.See it