簡體   English   中英

不使用正則表達式的Java中的正則表達式匹配

[英]regular expression matching in java without using regex

我最近遇到了一個采訪問題,我需要在Java中實現正則表達式匹配而不使用正則表達式匹配。

給定一個輸入字符串(s)和一個模式(p),實現支持'。','+','*'和'?'的正則表達式匹配。


// (pattern, match) --> bool does it match
// operators:
// . --> any char
// + --> 1 or more of prev char
// * --> 0 or more of prev char
// ? --> 0 or 1 of prev char

// (abc+c, abcccc) --> True
// (abcd*, abc) --> True
// (abc?c, abc) --> True
// (abc.*, abcsdfsf) --> True

我想出了以下僅實現“。”的代碼。 和“ *”,但無法弄清楚如何實現其他功能:

  public static boolean isMatch(String s, String p) {
    if (p.length() == 0) {
      return s.length() == 0;
    }
    if (p.length() > 1 && p.charAt(1) == '*') { // second char is '*'
      if (isMatch(s, p.substring(2))) {
        return true;
      }
      if (s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
        return isMatch(s.substring(1), p);
      }
      return false;
    } else { // second char is not '*'
      if (s.length() > 0 && (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0))) {
        return isMatch(s.substring(1), p.substring(1));
      }
      return false;
    }
  }

另外,解決此問題的最佳方法是什么?

這是未經測試的代碼。 這個想法是我們跟蹤字符串和模式中的位置。 這不是嘗試擴展到完整的RE引擎的好方法(只考慮添加括號會發生什么),但是在這種情況下很好:

public static boolean isMatch (String p, String s, int pos_p, int pos_s) {
    if (pos_p == p.length()) {
        // We matched the whole pattern.
        return true;
    }
    else if (pos_s == s.length()) {
        // We reached the end of the string without matching.
        return false;
    }
    else if (pos_p == -1) {
        // Do we match the pattern starting next position?
        if (isMatch(p, s, pos_p + 1, pos_s + 1)) {
            return true;
        }
        else {
            // Try to match the pattern starting later.
            return isMatch(p, s, pos_p, pos_s + 1);
        }
    }
    else {
        char thisCharP = p.charAt(pos_p);
        char nextCharP = pos_p + 1 < p.length() ? p.charAt(pos_p + 1) : 'x';

        // Does this character match at this position?
        boolean thisMatch = (thisCharP == s.charAt(pos_s));
        if (thisCharP == '.') {
            thisMatch = true;
        }

        if (nextCharP == '*') {
            // Try matching no times - we don't need thisMatch to be true!
            if (isMatch(p, s, pos_p + 2, pos_s)) {
                return true;
            }
            else {
                // Try matching 1+ times, now thisMatch is required.
                return thisMatch && isMatch(p, s, pos_p, pos_s + 1);
            }
        }
        else if (nextCharP == '+') {
            if (! thisMatch) {
                // to match 1+, we have to match here.
                return false;
            }
            else if (isMatch(p, s, pos_p + 2, pos_s + 1)) {
                // We matched once.
                return true;
            }
            else {
                // Can we match 2+?
                return isMatch(p, s, pos_p, pos_s + 1);
            }
        }
        else if (thisMatch) {
            // Can we match the rest of the pattern?
            return isMatch(p, s, pos_p + 1, pos_s + 1);
        }
        else {
            // We didn't match here, this is a fail.
            return false;
        }
    }
}

public static boolean isMatch (String p, String s) {
    // Can we match starting anywhere?
    return isMatch(p, s, -1, -1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM