标签:iostream lan case .com print ext out pil ext2
对于两段文本
这句话里有two个word。
这句话里有tWo个WoRd。
我希望用同一个正则表达式将其中的单词two、word、tWo、WoRd提取出来
这时候就须要用到不区分大小写的匹配模式
C++示例
#include <iostream> #include <regex> using namespace std; int main() { string text = "这句话里有two个word。"; regex re("[a-z]+"); sregex_iterator itr1(text.begin(), text.end(), re); sregex_iterator itr2; for (sregex_iterator itr = itr1; itr != itr2; ++itr) { cout << itr->str() << endl; } string text2 = "这句话里有tWo个WoRd。"; regex re2("[a-z]+", regex_constants::icase); //不区分大小写的匹配 sregex_iterator beginIterator(text2.begin(), text2.end(), re2); sregex_iterator endIterator; for (sregex_iterator itr = beginIterator; itr != endIterator; ++itr) { cout << itr->str() << endl; } return 0; }
Java示例
import static java.lang.System.out; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegularExpressionDemo { public static void main(String[] args) { String text = "这句话里有tWo个WoRd。"; Pattern pattern = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { out.println(matcher.group()); } } }
标签:iostream lan case .com print ext out pil ext2
原文地址:https://www.cnblogs.com/buyishi/p/10441143.html