本帖最后由 jingzhouheima 于 2013-8-31 23:35 编辑
package com.horizon.basejava;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex
{
public static void main(String[] args)
{
regspilt();
}
private static void regspilt() {
String str = "sd哈呀国际gh";
//int n = 2;
//String regex = "(.)\\1{"+n+"}";
String regex = "(.)\\1{2}";
Pattern p = Pattern.compile(regex);//将规则封装成Pattern对象
Matcher m = p.matcher(str);//将规则与要作用的字符串向关联
while(m.find());//先去找,找到的结果会自动放在group()中
{
System.out.println(m.group());
}
}
我的想法是:从"sd哈呀国际gh"中按字节数截取一部分,但不能截取出半个中文
我想用正则的方法做...........
程序运行的结果是:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:468)
at java.util.regex.Matcher.group(Matcher.java:428)
at com.horizon.basejava.Regex.regspilt(Regex.java:24)
at com.horizon.basejava.Regex.main(Regex.java:13)
----------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------求分析思路-----------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
|