日期:2014-05-20  浏览次数:20836 次

关于正则表达式提取规则文本
有一行文本格式如下

张三\李四\王小二\Thomas\……\唐

如何把“张三”、“李四”、“王小二”、“Thomas”、……、“唐”抽出来?

------解决方案--------------------
Java code
        String test = "张三\\李四\\王小二\\Thomas\\……\\唐";
          Pattern p = Pattern.compile("(?<=^|\\\\)[^\\\\]+(?=\\\\|$)");
          Matcher m = p.matcher(test);
          while(m.find()){
              System.out.println(m.group());
          }

------解决方案--------------------
Java code

import java.util.Scanner;


public class Test {
    
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        String str=scanner.nextLine();
        System.out.println(str);
        String[] strs = str.split("\\\\");
        for(String temp:strs)
            System.out.println(temp);
    }
    

}

------解决方案--------------------
探讨
Java code
String test = "张三\\李四\\王小二\\Thomas\\……\\唐";
Pattern p = Pattern.compile("(?<=^|\\\\)[^\\\\]+(?=\\\\|$)");
Matcher m = p.matcher(test);
while(m.find(……

------解决方案--------------------
探讨

Java code

import java.util.Scanner;


public class Test {

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String str=scanner.nextLine……