C++/STL 字符串:如何使用通配符模拟正则表达式之类的函数?
我想使用通配符比较 4 个字符串。
例如:
std::string wildcards[]=
{"H? ", "RH? ", "H[0-5] "};
/*in the last one I need to check if string is "H0 ",..., and "H5 " */
是否可以只用STL来实现?
谢谢, 阿曼.
编辑:
我们可以在没有 boost.regex 的情况下做到这一点吗?
或者我应该向我的项目添加另一个库依赖项?:)
I would like to compare 4 character string using wildcards.
For example:
std::string wildcards[]=
{"H? ", "RH? ", "H[0-5] "};
/*in the last one I need to check if string is "H0 ",..., and "H5 " */
Is it possible to manage to realize only by STL?
Thanks,
Arman.
EDIT:
Can we do it without boost.regex?
Or should I add yet another library dependences to my project?:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Boost.Regex
Use Boost.Regex
不 - 你需要 boost::regex
No - you need boost::regex
正则表达式就是为这类事情而设计的。我可以理解您不愿意避免依赖,但在这种情况下,这可能是合理的。
您可以检查您的 C++ 编译器,看看它是否包含任何内置正则表达式库。例如,Microsoft 包含 CAtlRegExp。
除此之外,您的问题看起来编写自定义代码并不难。
Regular expressions were made for this sort of thing. I can understand your reluctance to avoid a dependency, but in this case it's probably justified.
You might check your C++ compiler to see if it includes any built-in regular expression library. For example, Microsoft includes CAtlRegExp.
Barring that, your problem doesn't look too difficult to write custom code for.
您可以在不引入新的库依赖项的情况下完成此操作,但这样做最终您需要自己编写正则表达式引擎(或至少是正则表达式引擎的子集)。
您有什么原因不想为此使用库吗?
You can do it without introducing a new library dependency, but to do so you'd end up writing a regular expression engine yourself (or at least a subset of one).
Is there some reason you don't want to use a library for this?