Java中的模式匹配器

Java中的模式匹配器,java,regex,Java,Regex,我想要一个像这样的模式匹配器的结果 finalResult = "1. <b>Apple</b> - Apple is a fruit 2. <b>Caw</b> - Caw is an animal 3. <b>Parrot</b> - Parrot is a bird"; finalResult=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”; 我试着这样做: String test

我想要一个像这样的模式匹配器的结果

finalResult = "1. <b>Apple</b> - Apple is a fruit 2. <b>Caw</b> - Caw is an animal 3. <b>Parrot</b> - Parrot is a bird";
finalResult=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
我试着这样做:

        String test = "1. Apple - Apple is a fruit 2. Caw - Caw is an animal 3. Parrot - Parrot is a bird";
        String finalResult = "";

        Pattern pat = Pattern.compile("\\d\\.(.+?)-");
        Matcher mat = pat.matcher(test);

        int count = 0;
        while(mat.find()){
            finalResult += test.replaceAll(mat.group(count), "<b>" + mat.group(count) + "</b>");
            count++;
        }
String test=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
字符串finalResult=“”;
Pattern pat=Pattern.compile(“\\d\\\.(.+?)-”);
匹配器垫=匹配器(测试);
整数计数=0;
while(mat.find()){
finalResult+=test.replaceAll(材料组(计数),“+mat.group(计数)+”);
计数++;
}

您可以使用
Matcher
类的
replaceAll
方法。()

代码:

String test=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
字符串finalResult=“”;
Pattern pat=Pattern.compile(“(\\d+\\\.\\s(+?)\\s-”);
匹配器垫=匹配器(测试);
if(mat.find()){
最终结果=材料替换全部($1.$2-);
}
系统输出打印LN(最终结果);
replace all
用指定的正则表达式替换字符串的所有匹配项<代码>$1和
$2
是捕获的组(例如列表的第一个元素为“1”和“Apple”)

我稍微更改了你的正则表达式:

  • (\\d+)
    捕获多数字(不仅仅是0-9)。此外,它还将其“保存”在组1中
  • 添加了与空白符号匹配的
    \\s
    符号

  • 您可以使用
    Matcher
    类的
    replaceAll
    方法。()

    代码:

    String test=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
    字符串finalResult=“”;
    Pattern pat=Pattern.compile(“(\\d+\\\.\\s(+?)\\s-”);
    匹配器垫=匹配器(测试);
    if(mat.find()){
    最终结果=材料替换全部($1.$2-);
    }
    系统输出打印LN(最终结果);
    
    replace all
    用指定的正则表达式替换字符串的所有匹配项<代码>$1和
    $2
    是捕获的组(例如列表的第一个元素为“1”和“Apple”)

    我稍微更改了你的正则表达式:

  • (\\d+)
    捕获多数字(不仅仅是0-9)。此外,它还将其“保存”在组1中
  • 添加了与空白符号匹配的
    \\s
    符号

  • 您可以直接使用
    test.replaceAll()
    而不是使用
    Pattern.matcher()
    ,因为
    replaceAll()
    本身接受正则表达式


    要使用的正则表达式类似于
    ”(?您可以直接使用
    test.replaceAll()
    而不是使用
    Pattern.matcher()
    ,因为
    replaceAll()
    自己接受正则表达式


    要使用的正则表达式类似于
    ”(?@Codebender的解决方案更紧凑,但您始终可以使用
    String.split()
    方法:

        String test = "1. Apple - Apple is a fruit 2. Caw - Caw is an animal 3. Parrot - Parrot is a bird";
    
        String[]tokens = test.split("-\\s*|\\d\\.\\s*");
        StringBuffer result = new StringBuffer();
        int idx = 1;
        while (idx < (tokens.length - 1))
        {
            result.append("<b>" + tokens[idx++].trim() + "</b> - " + tokens[idx++].trim() + ". ");
        }
        System.out.println(result);
    
    String test=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
    String[]tokens=test.split(“-\\s*|\\d\\.\\s*”;
    StringBuffer结果=新的StringBuffer();
    int-idx=1;
    而(idx<(tokens.length-1))
    {
    追加(“+tokens[idx++].trim()+”-“+tokens[idx++].trim()+”);
    }
    系统输出打印项次(结果);
    
    @Codebender的解决方案更紧凑,但您可以始终使用
    String.split()
    方法:

        String test = "1. Apple - Apple is a fruit 2. Caw - Caw is an animal 3. Parrot - Parrot is a bird";
    
        String[]tokens = test.split("-\\s*|\\d\\.\\s*");
        StringBuffer result = new StringBuffer();
        int idx = 1;
        while (idx < (tokens.length - 1))
        {
            result.append("<b>" + tokens[idx++].trim() + "</b> - " + tokens[idx++].trim() + ". ");
        }
        System.out.println(result);
    
    String test=“1.苹果-苹果是水果2.呱呱-呱呱是动物3.鹦鹉-鹦鹉是鸟”;
    String[]tokens=test.split(“-\\s*|\\d\\.\\s*”;
    StringBuffer结果=新的StringBuffer();
    int-idx=1;
    而(idx<(tokens.length-1))
    {
    追加(“+tokens[idx++].trim()+”-“+tokens[idx++].trim()+”);
    }
    系统输出打印项次(结果);
    
        String test = "1. Apple - Apple is a fruit 2. Caw - Caw is an animal 3. Parrot - Parrot is a bird";
    
        String[]tokens = test.split("-\\s*|\\d\\.\\s*");
        StringBuffer result = new StringBuffer();
        int idx = 1;
        while (idx < (tokens.length - 1))
        {
            result.append("<b>" + tokens[idx++].trim() + "</b> - " + tokens[idx++].trim() + ". ");
        }
        System.out.println(result);