【问题标题】:How do I break a string into sections of n word groupings using regex and split?如何使用正则表达式将字符串分成 n 个单词分组的部分并拆分?
【发布时间】:2018-07-21 20:27:49
【问题描述】:

我正在开发抄袭检测软件的一部分,需要使用正则表达式和拆分方法将字符串拆分为单词子组。

假设我们有以下字符串,并希望将其分成三个单词。在这种情况下 split(regex) 应该在每三个空格之后分割句子。

样本数据: "It is a long established fact that"
样本输出: "It is a", "long established fact"

这是代码的简化版本,包括我正在处理的部分。我设法在每两个词之后拆分,但在 n=3 时无法拆分。

public class String {
    public void Splitter(String string){
    //string:"It is a long established fact that"
    String[] splitString =string.split("(?<!\\G\\S+)\\s");
    }
}

以上代码的输出如下:

splitString[0] = "It is"
splitString[1] = "a long"
splitString[2] = "established fact"

然后我想出了这个正则表达式(?&lt;=\\G\\s{2})\\s“如果前面还有两个空格,则匹配每个空格。”并期望输出为"It is a", "long established fact",但数组为空。

这是我刚刚构建的另一个正则表达式:("(?&lt;=(^|\\G)\\S*\\s\\S*\\s\\S*)\\s") 它几乎可以完成这项工作。唯一的问题是,如果句子中的单词总数不能被 n 整除,那么最后一组单词可以包含少于 n 个单词,splitString[3] = "that"

【问题讨论】:

  • 我宁愿用 whitechar 拆分所有内容并从中形成所需的组
  • (?&lt;=\s+\S+\s+\S+)\s 但它是可变长度的后视。您应该使用 \s*(\S+\s+\S+\s+\S+)(?=\s|$)|.+ 匹配所有内容,这将匹配字符串的每个部分。
  • @Antoniossss 我也这么认为。但我宁愿使用 split(regex) 代替。
  • @sln 我尝试了你对正则表达式的两个建议,但第一个返回了相同的字符串,第二个返回了一个空的字符串数组。

标签: java regex split


【解决方案1】:

你不能使用 split 函数,split 方法将这个字符串拆分为给定正则表达式的匹配项,并且你没有 de 分隔符的条件,你有一个字符串在 de 分隔符之间的条件 "\S \s+\S\s+\S",你的方法不对。

如果您需要为此使用正则表达式,请使用 PatternMatcher 类。

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public static void main(String[] args) {
    // TODO Auto-generated method stub
     Pattern p = Pattern.compile("\\S+\\s+\\S+\\s+\\S+\\s*|\\S+\\s*$|\\S+\\s+\\S+\\s*$");
     Matcher m = p.matcher("It is a long established fact that");
     String palabras=null;
     do {
        try {
            m.find(); 
            palabras = m.group();
            System.out.println(palabras);
        } catch(IllegalStateException E) {
            break;
        }
     } while(null != palabras && "" != palabras);
}

输出:

It is a 
long established fact
that

“n”字具有相同路径的通用正则表达式,其中 3 是您的 n,2 是您的 n-1。

"(\\S+\\s+){3}\\s*|(\\S+\\s*){1,2}$"

替换之前的代码:

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

public class RegexTestPalabra {

    public static void main(String[] args) {
         int n = 3;
         String regexPat = String.format("(\\S+\\s+){%d}\\s*|(\\S+\\s*){1,%d}$", n,n-1);

         Pattern p = Pattern.compile(regexPat);
         Matcher m = p.matcher("It is a long established fact that is ");
         String palabras=null;
         do{
            try{
            m.find(); 
            palabras = m.group();
            System.out.println(palabras);
            }catch(IllegalStateException E){
                break;
            }
         }while(null != palabras && "" != palabras);

    }

}

【讨论】:

    【解决方案2】:

    如果你想改变它。这只是一个示例,您可以如何将 String 语句分解为单独的单词,这些单词将存储在 ArrayList 中。

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    private static String word="";
    private static int readerStoppedAt=0;
    private static int h;
    private static List<String> strings = new ArrayList<>();
    
    public static void main(String[] args) {
          String text;
    
        //try one of those Strings:
        //text="Hello there, it's just a casual test!";
        //text="It works!!%%%!!||#@|''#'@ Symbols are not a problem";
        text="Good game, well played! Future changes are not necessary!";
    
        String text2=removeSymbols(text);
        for(int i=0; i<text2.length();i++){
            h=i; //h=where main cycle is currently at
            char c=text2.charAt(i);
            String str=c+"";
            if(str.equals(" ")){
                check(text2, 0);
            }else if(i+1==text2.length()){
                check(text2, 1);
            }
        }
        for (String string : strings) {
            System.out.println(string);
        }
    }
    
    private static String removeSymbols(String text) {
        //You can add some symbols if you want
       text=text.replace("!","");
       text=text.replace(",","");
       text=text.replace("@","");
       text=text.replace("#","");
       text=text.replace("|","");
       text=text.replace("''","");
       text=text.replace("'","");
       text=text.replace("%","");
        text=text.replace(":","");
        text=text.replace("(","");
        text=text.replace(")","");
        text=text.replace("{","");
        text=text.replace("}","");
        return text;
    }
    
    private static void check(String text, int inc) {
        for(int j=readerStoppedAt;j<h+inc;j++){
            char temp = text.charAt(j);
            String tempStr= temp +"";
            if(!tempStr.equals(" ")){
                word=word+ temp;
            }
        }
        readerStoppedAt=h;
        strings.add(word);
        word="";
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2019-05-14
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 2021-03-06
      • 1970-01-01
      相关资源
      最近更新 更多