关于正则表达式过滤字符串,PatternSyntaxException: Illegal repetition {.*}

来源:百度知道 编辑:UC知道 时间:2024/07/08 02:31:05
String reg = "{.*?}"; //特殊字符

Pattern ptt = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);

Matcher m_other = ptt.matcher("{hezzla}你好");

System.out.println(m_other.replaceAll(""));

我想输出的文字是:“你好”

而不要带大括号的英文,注意,是英文,如果是汉字,那么不过滤。这个正则应该怎么写?
还是不行,这样的就过滤不了:
{insaasassa-a} 我需要过滤特殊符号
{insaasassa aaaa} 另外还需要过滤里面的空格啊 (你们的正则这个一直没有达到要求,2楼的达到了这个要求,但是带中文也给过滤了)
另外,这里如果有中文,那么就不过滤如{aashhdk啊},这样的不过滤

String s = "{xx-}english{汉字}汉字";
System.out.println(s.replaceAll("\\{[a-zA-Z-]+\\}", ""));

///////如果大括号是边界符那么要转义~~ ,已经重新修改,满足要求啦,你自己看看吧~~~
//////////////
import java.util.regex.*;

public class Test {

public static void main(String[] args) {
///关键是这里
String reg = "\\{[\\x00-\\xff]*\\}";
Pattern ptt = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher m_other = ptt.matcher("{hezzla}你好\n{hezzla哈}你好");
System.out.println(m_other.replaceAll(""));
}

}

试试这2种.
String reg = "[ -~]"; //特殊字符
String reg = "[^\x00-\xff]" //非中文字符