Equivalent to matcher.start and matcher.end in C #

I am new to C # and am trying to convert below code to C # but cannot find api in C # to do this.

  • Please explain the equivalent of matcher.start () and matcher.end ().
  • which is equivalent to matcher.group () in C #.

    private String getFinalVariant(String strInputWord) {
    
    Pattern pat = Pattern.compile("[aeiouhy]+");
    Matcher mat = pat.matcher(strInputWord);
    int lenghtOfInputWord = strInputWord.length();
    while (mat.find()) {
        if (mat.start() == 0) {
    
            int index = '1';
            Map<String, String> temp = ruleList[index];
            if (temp.containsKey(mat.group())) {
                strInputWord = strInputWord.replaceFirst(mat.group(), temp.get(mat.group()));
            }
    
        } else if (mat.end()== lenghtOfInputWord) {
    
            int index = '3';
            int lastIndex = strInputWord.lastIndexOf(mat.group());
            Map<String, String> temp = ruleList[index];
            if (temp.containsKey(mat.group())) {
                String tail = strInputWord.substring(lastIndex).replaceFirst(mat.group(), temp.get(mat.group()));
                strInputWord = strInputWord.substring(0, lastIndex) + tail;
            }
    
        } else {
    
            int index = '2';
            Map<String, String> temp = ruleList[index];
            String str = mat.group();
            // System.out.println(str);
            //  System.out.println(mat.start());
            if (temp.containsKey(mat.group())) {
                if (strInputWord.length() > 3) {
                    int index1 = strInputWord.indexOf(mat.group(), 1);
                    if (index1 != 0 && index1 != strInputWord.length() - 1) {
                        String matched = strInputWord.substring(index1, strInputWord.length() - 1).replaceFirst(mat.group(), temp.get(mat.group()));
                        strInputWord = strInputWord.substring(0, index1) + matched + strInputWord.charAt(strInputWord.length() - 1);
                    }
    
                } else if (strInputWord.length() == 3) {
                    strInputWord = strInputWord.charAt(0) + strInputWord.substring(1, 2).replaceFirst(mat.group(), temp.get(mat.group())) + strInputWord.charAt(strInputWord.length() - 1);
                }
    
            }
    
        }
    
    }
    return strInputWord;
    }
    
          

+3


source to share


1 answer


you can use

var matches = Regex.Matches(yourString);

      



This will return all matches. Each match is a Match object that has an Index property that you can use to design the first and last matches.

For group (), you can use the same collection to repeat different matches.

+1


source







All Articles