以下是我的计划。
我尝试用空格(@
)替换&
,;
,.
和。
但我无法得到正确的输出。
public static void main(String[] args) throws IOException {
final String REG_EXPR = "[!\"#$%&'()*+-./:;<=>?@\\^_{|}~`,.\\[\\]–]*";
String Description="திகழ்கிறது.@;@;@;@;ஆனால் ஆனால் ";
HashSet<String> line=new HashSet<String>();
Scanner sc2 = null;
sc2 = new Scanner(Description);
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
boolean b;
while (s2.hasNext()) {
String s = s2.next();
String s1 = s.replaceAll(REG_EXPR, "");
s1=s1.replaceAll("[a-zA-Z0-9\\s]", "");
System.out.println("s1 value"+s1);
}
}
}
输出
s1 valueதிகழ்கிறதுஆனால்
s1 valueஆனால்
预期产出
s1 valueதிகழ்கிறது
s1 valueஆனால்
s1 valueஆனால்
答案 0 :(得分:1)
这个对我来说很好用:
public static void main(String[] args) {
final String REG_EXPR = "[!\"#$%&'()*+-./:;<=>?@\\^_{|}~`,.\\[\\]–]";
String description="திகழ்கிறது.@;@;@;@;ஆனால் ஆனால் ";
String s1 = description.replaceAll(REG_EXPR," ");
Scanner scanner = new Scanner(s1);
while (scanner.hasNext()){
System.out.println("s1 value " + scanner.next());
}
}
答案 1 :(得分:0)
尝试并执行此操作:
private static final Pattern PATTERN = Pattern.compile("\\p{IsL}+");
// ...
final Matcher m = PATTERN.matcher(description);
while (m.find())
System.out.println(m.group());
答案 2 :(得分:0)
扫描仪类处理存在问题
你得到的第一首字符串是“திகழ்கிறது。@; @; @; @;ஆனால்”,它正确地正在进行正则表达式,预期的输出是“திகழ்கிறதுஆனால்”
在这里给一个空间“திகழ்கிறது。@; @; @; @;ஆனால்”并尝试,你会得到正确的输出。
试试这个,
final String REG_EXPR =“[!\”#$%&amp;'()* + - ./ :;&lt; =&gt;?@ \ ^ _ {|}〜`,。\ [\] - a -Za-Z0-9 \\ S]“;
用下面的
替换while循环 while (s2.hasNext()) {
String s = s2.next();
String []s1 = s.split(REG_EXPR);
for(int i=0 ;i<s1.length;i++ )
if(s1[i].length()>0)
System.out.println("s1 value"+s1[i]);
}
答案 3 :(得分:-2)
请记住将*+?
转换为\\*\\+\\?
等特定于正则表达式的字符。否则,这些将用于确定匹配样本的长度。即?
表示occurs zero or one times
,+
表示one or more times
,*
表示any times (zero, one or more)
。