Java_正则处理字符串

使用java中的Matcher,Pattern,LinkedList,Iterator类

要求

字符串。一篇英文文章包含有英文单词、数字、各种标点符号以及空格。分别统计出单词个数和数字个数,重复出现的单词和数字只计一次,并且输出单词和数字列表。

参考

完成

import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test4_2 {
  public static void main(String[] args) {
    String tagString = new String(
        "Almost every child will complain about their parents sometimes.It is natural, because when people stay together for a long time, they will start to have argument. But ignore about the unhappy time, our parents love us all the time. No matter what happen to us, they will stand by our sides. We should be grateful to them and try to understand them.");
    String wordRegex = "[A-Za-z]+";//单词的正则匹配
    String signRegex = "[\\p{Punct}]";//符号的正则匹配
    String numRegex = "[0-9]+";//数字的正则匹配
    int countB = 0;

    LinkedList<String> wordList = matchList(tagString, wordRegex);//处理单词
    LinkedList<String> signList = matchList(tagString, signRegex);//处理符号
    LinkedList<String> numList = matchList(tagString, numRegex);//处理数字
    countB = countBlank(tagString);//处理空格
    System.out.println("一共有空格数:" + countB);
    //单词,标点符号,数字的处理思路
    //把所有正则能匹配到的字符串添加到列表
    //然后取出第一个单词,在列表中遍历这个单词,计数器工作,每搜寻到一个匹配,从列表中删除这个节点
    //最后,计数器返回该单词的个数,链表中也没有了这个单词,循环开始处理下一个单词 
    while (wordList.size() != 0) {
      String temp = wordList.getFirst();
      int count = 0;
      while (wordList.removeFirstOccurrence(temp)) {
        count++;
      }
      System.out.printf("%12s一共出现了%d次\n", temp, count);
    }
    while (signList.size() != 0) {
      String temp = signList.getFirst();
      int count = 0;
      while (signList.removeFirstOccurrence(temp)) {
        count++;
      }
      System.out.printf("%12s一共出现了%d次\n", temp, count);
    }
    while (numList.size() != 0) {
      String temp = numList.getFirst();
      int count = 0;
      while (numList.removeFirstOccurrence(temp)) {
        count++;
      }
      System.out.printf("%12s一共出现了%d次\n", temp, count);
    }

  }

  static LinkedList<String> matchList(String tagString, String regex) {
    //从字符串中寻找匹配正则的项,将所有匹配到的项做成一个单链表返回
    LinkedList<String> list = new LinkedList<String>();
    Pattern r = Pattern.compile(regex);
    Matcher matcher = r.matcher(tagString);
    while (matcher.find()) {
      list.add(matcher.group());
    }
    return list;
  }

  static int countBlank(String tagString) {
    //计数所有的空白符号
    int b = 0;
    Pattern r = Pattern.compile("\\s");
    Matcher matcher = r.matcher(tagString);
    while (matcher.find()) {
      b++;
    }
    return b;
  }
}

Leave a Comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注