正则这个东西隔几天不用就容易忘记,所以把刷LeetCode时候碰到的能用正则表达式解决的题目全都记在这里,供以后参考使用,这里面有的是直接写出正则表达式即可,我标为“纯正则”,有的则需要根据相关字符串构造出正则表达式,并配合别的函数使用,欢迎补充,持续更新~
注意:出现在此处的题目并不是一定用正则表达式是最优解法,某些可能处于复杂度边缘,某些(其实就是指实现正则的题)相对于正解有投机取巧之嫌,此处仅仅为我个人做正则练习使用
顺便挂几个我自己在用的正则表达式测试网站,供参考
regExr
regex101
10. 正则表达式匹配
class Solution {
public:
bool isMatch(string s, string p) {
return regex_match(s, regex(p));
}
};
36. 有效的数独
regex re(R"(([1-9])[.1-9]*\1|([1-9])(?:[\n.1-9]{10})*[\n.1-9]{9}\2|^(?:(?:[.1-9]{9}\n){3})*(?:[.1-9]{3})*[.1-9]{0,2}([1-9])(?:[.1-9]?|[.1-9]{0,2}(?:\n[.1-9]{6}|[.1-9]{3}\n[.1-9]{3}|[.1-9]{6}\n)(?:[\n.1-9]{10})?)[.1-9]{0,2}\3|^(?:(?:[.1-9]{9}\n){3})*[.1-9]{9}\n(?:[.1-9]{3})*[.1-9]{0,2}([1-9])(?:[.1-9]?|[.1-9]{0,2}(?:\n[.1-9]{6}|[.1-9]{3}\n[.1-9]{3}|[.1-9]{6}\n))[.1-9]{0,2}\4)");
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
return !regex_search(accumulate(board.cbegin(), board.cend(), ""s, [](auto&& s, auto&& v) { return s + accumulate(v.begin(), v.end(), ""s) + '\n'; }), re);
}
};
408. 有效单词缩写
regex zero("(?:^|\\D)0|[^\\D12]\\d");
regex num("(\\d+)");
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
return !regex_search(abbr, zero) && regex_match(word, regex(regex_replace(abbr, num, ".{$1}")));
}
};
459. 重复的子字符串
regex re("(.*)\\1+");
class Solution {
public:
bool repeatedSubstringPattern(string s) {
return regex_match(s, re);
}
};
468. 验证IP地址
regex ipv4("^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)($|(?!\\.$)\\.)){4}$");
regex ipv6("^(?:(?:[\\da-fA-F]{1,4})($|(?!:$):)){8}$");
class Solution {
public:
string validIPAddress(string queryIP) {
if (regex_search(queryIP, ipv4)) return "IPv4";
else if (regex_search(queryIP, ipv6)) return "IPv6";
else return "Neither";
}
};
591. 标签验证器
regex tag(R"(<([A-Z]{1,9})>[^<]*\1>)");
regex cdata(R"()");
class Solution {
public:
bool isValid(string code) {
code = regex_replace(code, cdata, "#");
for (string prev; prev != code; prev = exchange(code, regex_replace(code, tag, "~")));
return code == "~";
}
};
890. 查找和替换模式
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
int dict[26]{};
string tmp, pre("(?!\\1");
for (int cnt{}; auto & i : pattern)
{
if (dict[i - 'a']) tmp += "\\" + to_string(dict[i - 'a']);
else
{
if (cnt) {
if (cnt != 1) pre += "|\\" + to_string(cnt);
tmp += pre + ")";
}
tmp += "(\\w)";
dict[i - 'a'] = ++cnt;
}
}
vector<string> ans;
copy_if(words.cbegin(), words.cend(), back_inserter(ans), [reg = regex(tmp)](auto&& word) { return regex_match(word, reg); });
return ans;
}
};
1023. 驼峰式匹配
class Solution {
public:
vector<bool> camelMatch(vector<string>& queries, string pattern) {
vector<bool> ans;
regex re(accumulate(pattern.cbegin(), pattern.cend(), "[a-z]*"s, [](auto&& s, char c) { return s + c + "[a-z]*"; }));
transform(queries.cbegin(), queries.cend(), back_inserter(ans), [&re](auto&& s) { return regex_match(s, re); });
return ans;
}
};
1108. IP 地址无效化
regex re("\\.");
class Solution {
public:
string defangIPaddr(string address) {
return regex_replace(address, r, "[.]");
}
};
1455. 检查单词是否为句中其他单词的前缀
class Solution {
public:
int isPrefixOfWord(string sentence, string searchWord, int ans = 0) {
istringstream iss(sentence);
return any_of(istream_iterator<string>(iss), istream_iterator<string>(), [&, tmp = regex(searchWord + ".*")](auto&& s) { ans++; return regex_match(s, tmp); }) ? ans : -1;
}
};
2047. 句子中的有效单词数 顺便吐槽一下,本题不用正则极其恶心。。
regex re("(?:[a-z]+(?:-[a-z]+)?)?[!,.]?$");
class Solution {
public:
int countValidWords(const string& sentence) {
stringstream ss(sentence);
return count_if(istream_iterator<string>(ss), istream_iterator<string>(), [](auto&& s) { return regex_match(s, re); });
}
};
2299. 强密码检验器 II
regex re(R"((?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-+])(?!.*(.)\1).{8,})");
class Solution {
public:
bool strongPasswordCheckerII(string password) {
return regex_match(password, re);
}
};