在C ++中使用正则表达式检查字符串 [英] Checking strings with regex in C++

查看:635
本文介绍了在C ++中使用正则表达式检查字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在C ++应用程序中使用正则表达式,但我在regex中没有经验。我想特别检查一些字符串,如果他们属于以下类别之一:

I am going to use regular expressions in a C++ application, but I am not experienced in regex. I want in particular to check a number of strings if they belong to one of the following categories:

X.anystring - > X必须是必然和专有的字母)。

X.anystring -> X must be necessarily and exclusively letter (not digit).

XY.anystring - > X,Y必须是数字0-9(不是字母)。

XY.anystring -> X, Y must be necessarily and exclusively digits 0-9 (not letters).

如何使用正则表达式检查它们?

How can I check them using regex? What tutorial for regex could you recommend in order to acquaint me with regex?

推荐答案

很奇怪,regexp:s不是正确的解决方案

Seriously, regexp:s are not the right solution for you in this case.

首先,regexp:s不是C ++语言的一部分,因此您需要使用特定的regexp库。 (C ++ 11,无论谁支持regexp:s。)

To start with, regexp:s are not part of the C++ language so you will need to use a specific regexp library. (C++11, whoever, include support for regexp:s.)

其次,你的用例都可以用纯C ++编码,所有你需要做的是循环字符串中的字符,并检查它们是否符合您的要求。

Secondly, both your use cases can trivially be encoded in plain C++, all you need to do is to loop over the characters in the strings and check if each of them match your requirements.

这篇关于在C ++中使用正则表达式检查字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆