简体   繁体   English

在C ++中有一个使用模式匹配(使用正则表达式)的函数吗?

[英]There is a function to use pattern matching (using regular expressions) in C++?

There is a simple C++ method to use pattern matching on strings? 有一个简单的C ++方法在字符串上使用模式匹配? The code should sound like this: 代码听起来像这样:

if (regexpcmp("l?nole*[0-9]", "linoleum1")) {
  //we have a match!
} else {
   //no match 
}

Did you already look at Boost.Regex ? 你有没看过Boost.Regex

const boost::regex e("l?nole*[0-9]");
if (regex_match("linoleum1", e)) {
  //we have a match!
} else {
  //no match 
}

Not in the core language. 不是核心语言。 Use Boost.Regex or an external library like pcre . 使用Boost.Regex或像pcre这样的外部库。 In a unix environment you almost certainly have access to the BSD regular expression tools ( regcomp , regerror , regexec , regfree ) which are c-like rather than c++-like but do work. 在unix环境中,你几乎肯定可以访问BSD正则表达式工具( regcompregerrorregexecregfree ),这些工具是c-like而不是c ++,但是可以工作。

Take boost.regex friend. 以boost.regex的朋友为例。 if you are not allowed to use boost (sadly, there are still companies doing this), you could look into pcrecpp , which is a C++ binding developed by google for the famous PCRE library. 如果你不被允许使用boost(遗憾的是,仍有公司这样做),你可以查看pcrecpp ,这是由谷歌为着名的PCRE库开发的C ++绑定。

如果您使用最常用编译器的最新版本,则可以在TR1名称空间中使用标准正则表达式库(基于boost :: regex):std :: tr1 :: regex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM