简体   繁体   English

使用正则表达式匹配字符串

[英]Matching string using regex

I want to write a regular expression in a C++ program which checks if a string matches the following expression: 我想在C ++程序中编写一个正则表达式,它检查字符串是否与以下表达式匹配:

a word not containing '_' but it can contain number followed by 一个不包含'_'的单词,但它可以包含数字后跟

'_' followed by '_' 其次是

three digits in a row (ie 047) 连续三位数(即047)

followed by '_' followed by 然后是'_',然后是

a string (can contain anything) 字符串(可以包含任何内容)

I have tried the following expression but it does seem to find the desired string as described above. 我已经尝试了以下表达式,但似乎确实找到了如上所述的所需字符串。 I suspect the problem lies in the first part but I cannot detect it in order to modify properly: 我怀疑问题出在第一部分,但我无法检测到它以便正确修改:

static const wregex stringForm("([^_]?)_?(\\d{3})_(.+)");  

What is then the proper reg expression? 什么是正确的reg表达式?

\b[^_]*?(_\d{3}.+?)?\b

A word ( \\b is word boundary, quantifiers are non-greedy). 一个词( \\b是单词边界,量词是非贪婪的)。 Zero or more characters that aren't _ ( [^_]*? ). 零个或多个不是_( [^_]*? )的字符。 Optionally ( (...)? ), the digit sequence you described ( _\\d{3} ) followed by one or more of any character ( .+? ). 可选( (...)? ),您描述的数字序列( _\\d{3} )后跟一个或多个任何字符( .+? )。

你试过这个:

static const wregex stringForm("([a-zA-Z0-9]*_[0-9]{3}.*)"); 

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

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