C++正则表达式的简单使用
我只是想搞乱并熟悉在 C++ 中使用正则表达式。 假设我希望用户输入以下内容:###-$$-###,使 #=0-9 之间的任何数字,$=0-5 之间的任何数字。这是我实现此目的的想法:
regex rx("[0-9][0-9][0-9]""\\-""[0-5][0-5]")
这不是确切的代码,但这是检查用户的输入是否是有效的数字字符串的一般想法。但是,假设我不允许以 0 开头的数字,因此:099-55-999 是不可接受的。我如何检查类似的内容并输出无效?谢谢
I'm just trying to mess around and get familiar with using regex in c++.
Let's say I want the user to input the following: ###-$$-###, make #=any number between 0-9 and $=any number between 0-5. This is my idea for accomplishing this:
regex rx("[0-9][0-9][0-9]""\\-""[0-5][0-5]")
That's not the exact code however that's the general idea to check whether or not the user's input is a valid string of numbers. However, let's say i won't allow numbers starting with a 0 so: 099-55-999 is not acceptable. How can I check something like that and output invalid? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
匹配以 0 到 9 之间的三位数字开头、后跟破折号、0 到 5 之间的两位数字、破折号、0 到 9 之间的三位数字开头的字符串。
这是您要查找的吗?这是非常基本的正则表达式内容。我建议您查看不错的教程。
编辑:(在您更改问题后):
将与上面相同的匹配,但不允许 0 作为第一个字符。
matches a string that starts with three digits between 0 and 9, followed by a dash, followed by two digits between 0 and 5, followed by a dash, followed by three digits between 0 and 9.
Is that what you're looking for? This is very basic regex stuff. I suggest you look at a good tutorial.
EDIT: (after you changed your question):
would match the same as above except for not allowing a 0 as the first character.
您谈论的是在 c++ 中使用 tr1 正则表达式,而不是托管 c++?如果是这样,请访问此处,其中解释了这些内容。
另外,您应该知道,如果您使用 VS2010,则不再需要正则表达式的 boost 库。
Your talking about using tr1 regex in c++ right and not the managed c++? If so, go here where it explains this stuff.
Also, you should know that if your using VS2010 that you don't need the boost library anymore for regex.
试试这个:
有关解释,请检查@Tim 的答案。 请注意数字元字符的两个
\
。Try this:
For explanation check @Tim's answer. Do note the double
\
for the digit metacharacter.