如何为这个txt文件编写java正则表达式

如何为这个txt文件编写java正则表达式,java,regex,Java,Regex,谢谢你的帮助 我希望在txt文件中获得每个项目的Id和类别,如下所示: Id: 0 ASIN: 0771044445 discontinued product Id: 1 ASIN: 0827229534 title: Patterns of Preaching: A Sermon Sampler group: Book salesrank: 396585 similar: 5 0804215715 156101074X 0687023955 068707423

谢谢你的帮助

我希望在txt文件中获得每个项目的Id和类别,如下所示:

Id:   0
ASIN: 0771044445
discontinued product

Id:   1
ASIN: 0827229534
  title: Patterns of Preaching: A Sermon Sampler
  group: Book
  salesrank: 396585
  similar: 5  0804215715  156101074X  0687023955  0687074231  082721619X
  categories: 2
   |Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Preaching[12368]
   |Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Christianity[12290]|Clergy[12360]|Sermons[12370]
  reviews: total: 2  downloaded: 2  avg rating: 5
    2000-7-28  cutomer: A2JW67OY8U6HHK  rating: 5  votes:  10  helpful:   9
    2003-12-14  cutomer: A2VE83MZF98ITY  rating: 5  votes:   6  helpful:   5

Id:   2
ASIN: 0738700797
  title: Candlemas: Feast of Flames
  group: Book
  salesrank: 168596
  similar: 5  0738700827  1567184960  1567182836  0738700525  0738700940
  categories: 2
   |Books[283155]|Subjects[1000]|Religion & Spirituality[22]|Earth-Based Religions[12472]|Wicca[12484]
......
结果应组织为:

1 Book

2 Book

3 Book
然后,我编写了一个java程序来提取信息:

class Main
{

  public static void main(String[] args) throws IOException
  { 

    String file="/Users/swing/Desktop/test.rtf";  

      BufferedReader br;

      try 
      {
          br = new BufferedReader(new FileReader(file));

          String line;      

          String re1=".*?"; // Non-greedy match on filler
          String re2="";    // ID 1

          String re3="((?:[c-z][a-z]+))";   // Category 1

          Pattern p = Pattern.compile(re1+re2+re3,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
          Matcher m = p.matcher(file);

          while((line=br.readLine())!=null)
          {
            m=p.matcher(line);

              if (m.find())
              {
                  String id1=m.group(1);
                  String category1=m.group(2);
                  System.out.print(" "+id1.toString()+" "+" "+category1.toString()+" "+"\n");
              }    
          } 
      }  
      catch (FileNotFoundException e)    
      {         
          e.printStackTrace();  
          System.out.println("fail");}   
      }
}
由于我没有使用java正则表达式的经验,所以结果是错误的,您能帮我纠正错误的代码吗?谢谢

错误输出:

\r  tf 

\font  tbl 

color  tbl 

ar  gl 

ardir  natural 

ardir  natural 

AS  IN 

dis  continued 

AS  IN 

tit  le 

gro  up 

ales  rank 

simi  lar 

....

尝试使用此正则表达式和您已选择的选项(表示dotall和不区分大小写):

模式

Id:\s+?(\d).+?(?:group:|discontinued)\s(\w+?)\s
输入

您在问题中提供的.txt文件

输出

匹配项:

1. Group 1: 0
   Group 2: product

2. Group 1: 1
   Group 2: Book

3. Group 1: 2
   Group 2: Book

地球的最后一天。。。上帝会原谅你的son@user3045306每个项目的
类别是什么意思?您能根据示例输入为您想要的正则表达式提供正确的输出吗?@Tafari很抱歉给您带来不便,类别表示产品组,例如,对于产品ID 1,类别(组)是Book。谢谢您,当我将代码更改为:String ID=“\s+?(\d)。+?(?:group:|中止)\s(\w+)\s”;Pattern p=Pattern.compile(Id,Pattern.CASE不区分大小写| Pattern.DOTALL);Matcher m=p.Matcher(文件);它仍然不能工作。。。。我能换对吗?或者有一些语法错误…@user3045306可能您必须转义相同的字符,请尝试以下操作:String Id=“\\s+?(\\d)。+?(?:组:|中断)\\s(\\w+?)\\s”;如果我的回答对您有帮助,请接受:)String file=“/Users/swing/Desktop/test.rtf”;尝试读取整个文档并将其存储为一个字符串,然后尝试使用这一个字符串运行matcher,因为当您在每一行上使用该模式时,将不会得到任何结果。是否在每一行上尝试?对不起打扰你了。如果可能的话,你能给我看看密码吗?