我正在创建一个程序,它分析字符串以报告拼写错误的实例我希望它报告多个实例,而不是一个变量。例如,我让它解释用户输入;
咕咕咕咕叫
取这个字符串并报告所有“go”拼写错误4次的实例,因为如上面的用户条目所示,我们有“og”、“ug”、“gu”和“ig”。
所以我的结果应该是
y拼写错误x/计数次。
我不在乎图案反转部分。我只在使用单个变量时使用它来查找实例。

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

public class misspellReporter
{
  public static void main(String[] args)
  {
      Scanner keyboard = new Scanner(System.in);
      String singleString = "";
      System.out.println("Enter text here");
      singleString = keyboard.nextLine();

          String str = singleString;
          //String strToSearch = "OG"; //I used this at first
          String[] strToSearch = {"GU", "UG", "IG", "GI"}; //I want to use this array instead
          String strToSearchReversed = new StringBuffer(strToSearch).reverse().toString();
          Pattern strPattern = Pattern.compile(strToSearchReversed);
          Matcher matcher = strPattern.matcher(str);
          int counter = 0;
          while(matcher.find()) {
              ++counter;
          }

      System.out.println(strToSearch+" was spelt as "+strToSearchReversed+" "+counter+" times");
  }
}

提前谢谢你!这个问题对我来说不同的原因是因为我在论坛上没有看到其他人用matcher和patterns解析。我也用过其他方法,但这个方法有一个我感兴趣的具体操作。

最佳答案

可以使用这样的正则表达式同时搜索多个子字符串:

public class MatchPairs {
    private static final String[] strs = {"GU", "UG", "IG", "GI"};
    public static int matches( String str ){
        String strToSearch = String.join( "|", strs );
        Pattern strPattern = Pattern.compile(strToSearch);
        Matcher matcher = strPattern.matcher(str);
        int counter = 0;
        while(matcher.find()) {
             ++counter;
        }
        return counter;
    }
}

您可以通过反转组合并在另一个|之后附加它来省去添加反转子字符串的麻烦。
输出:
 GOOGGOUGGUIG was spelt as GU|UG|IG|GI 3 times

要避免重叠匹配,请设置开始偏移:
public class MatchNoOverlap {
    private static final String[] strs = {"GU", "UG", "IG", "GI"};
    public static int matches( String str ){
        String strToSearch = String.join( "|", strs );
        Pattern strPattern = Pattern.compile(strToSearch);
        Matcher matcher = strPattern.matcher(str);
        int counter = 0;
        int start = 0;
        while(matcher.find(start)) {
            ++counter;
            start = matcher.start() + 2;
        }
        return counter;
    }
    public static void main( String[] args ){
        System.out.println( matches( "GOOGGOUGGUGIGI" ) );
    }
}

以后
/* Counts the number of contiguous stretches of non-valid pairs between
 * contiguous stretches of valid pairs
 */
private static final String[] valids =
    {"AT", "TA", "AA", "TT", "CG", "GC", "CC", "GG"};

public static int mismatches( String str ){
    String strToSearch = "(?:(?:..)*?)((?:" + String.join( "|", valids) + ")+)";
    Pattern strPattern = Pattern.compile( strToSearch);
    Matcher matcher = strPattern.matcher(str);
    int counter = 0;
    int start = 0;
    int end = 0;
    while(matcher.find( start )){
        int s = matcher.start(1);
        end = matcher.end(1);
        if( s > start ){
            ++counter;
            // System.out.println( "s>Start "  + s );
        }
        // System.out.println( "match:" + matcher.group() +  " s=" + s );
        start = matcher.end();
    }
    if( end < str.length() ){
        ++counter;
        // System.out.println( "end< length" );
    }
    return counter;
}

**或者,计算每个“坏对”:
public static int badPairs( String str ){
    String strToSearch = "(?:(?:..)*?)((?:" + String.join( "|", valids) + ")+)";
    Pattern strPattern = Pattern.compile( strToSearch);
    Matcher matcher = strPattern.matcher(str);
    int counter = 0;
    int start = 0;
    int end = 0;
    while(matcher.find( start )){
        int s = matcher.start(1);
        end = matcher.end(1);
        counter += s - start;
        start = matcher.end();
    }
    counter += str.length() - end;
    return counter/2;
}

没有regex
public static int valid( String str ){
    Set<String> valset = new HashSet<>();
    for( String s: valids ) valset.add( s );
    int validCount = 0;
    for( int i = 0; i < str.length(); i += 2 ){
        if( valset.contains( str.substring( i, i+2 ) ) ) validCount++;
    }
    return validCount;
}

10-07 13:29