Java如何在正则表达式中匹配重复单词?在Java编程中,如何在正则表达式中匹配重复单词?
以下⽰例显⽰了如何使⽤regex.Matcher类的p.matcher()⽅法和m.group()⽅法在正则表达式中搜索重复的单词。
package com.yiibai;
import java.util.Scanner;
import java.io.*;
import *;
import java.util.ArrayList;
public class SearchingDuplicateWords {
public static void main(String[] args) {
ArrayList<String> manyLines = new ArrayList<String>();
ArrayList<String> noRepeat = new ArrayList<String>();
try {
String s1 = "Hello hello Hello there there past pastures ";
Scanner myfis = new Scanner(s1);
while (myfis.hasNext()) {
String line = Line();
String delim = Property("line.separator");
String[] lines = line.split(delim);
for (String s : lines) {
if (!s.isEmpty() && s != null) {
manyLines.add(s);
}
}
}
if (!manyLines.isEmpty()) {
System.out.print("Original text is:\n");
for (String s : manyLines) {
System.out.println(s);
}
}
if (!manyLines.isEmpty()) {
for (String s : manyLines) {
String result = s.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1");
noRepeat.add(result);
}
}
if (!noRepeat.isEmpty()) {
System.out.print("After Remove duplicates:\n");
for (String s : noRepeat) {
System.out.println(s);
}
}
} catch (Exception ex) {
System.out.println(ex);
}正则匹配怎么匹配单词
}
}
Java
上述代码⽰例将产⽣以下结果 -
Original text is:
Hello hello Hello there there past pastures
After Remove duplicates:
Hello there past pastures

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。