简体   繁体   English

正则表达式匹配包含括号的字符串

[英]regex to match string that contains brackets

I am reading in a file and verifying the contents of the file by checking each line. 我正在读取文件并通过检查每一行来验证文件的内容。 The string lines look like this: 字符串行如下所示:

CMD: [THIS_IS_THE_CMD]
DELAY: [5]
FLAGS: [ANY]

All I need to check is that the line follows that exact form and what is in between the brackets is either text (I have tried [A-Z_] but it's not working) or a number depending on the line. 我需要检查的是,该行遵循该确切的形式,括号之间的内容是文本(我已尝试[A-Z_]但它不起作用)或取决于行的数字。
What I have so far: 到目前为止我所拥有的:

string line = "CMD: [THIS_IS_THE_CMD]";
if(!VerifyLine(@"^CMD: \[", line))
{
     // No match, set error    
}  

private static bool VerifyLine(string regExp, string line)
{
    Regex reg = new Regex(regExp);
    return reg.IsMatch(line);
}  

But this does not check the contents in between the brackets and it does not check for the closing bracket. 但这不会检查括号之间的内容,也不会检查结束括号。

This should do it for you: 这应该为你做:

([A-Z_]*):\s*\[(\w*)\]

First group matches the part before the colon, second matches the part inside the []s. 第一组匹配冒号前的部分,第二组匹配[] s内的部分。

First part can be any uppercase letter or underscore, second part can be any alphanumeric character of any case, or an underscore. 第一部分可以是任何大写字母或下划线,第二部分可以是任何情况下的任何字母数字字符,或下划线。

Additionally, you might use the following extras, which require the option that makes ^$ match EOLs instead of just BOF and EOF: 此外,您可以使用以下附加功能,这些附加功能需要使^ $匹配EOL而不仅仅是BOF和EOF的选项:

^([A-Z_]*):\s*\[(\w*)\]$       // will only match whole lines
^\s*([A-Z_]*):\s*\[(\w*)\]\s*$ // same as above but ignores extra whitespace 
                               // on the beginning and end of lines

Different things you might use to capture the groups depending on your file format: 您可能会使用不同的东西来捕获组,具体取决于您的文件格式:

[A-Z]       // matches any capital letter
[A-Za-z]    // matches any letter
[A-Za-z0-9] // matches any alphanumeric character
\w          // matches any "word character", which is any alnum character or _

try with this: ^\\w+:\\s*\\[(\\w+)\\] , \\w will match alphabet, digit, and underscore 试试这个: ^\\w+:\\s*\\[(\\w+)\\]\\w将匹配字母,数字和下划线
and another pattern will match upper case only: ^[AZ\\d_]+:\\s*\\[([AZ\\d_]+)\\] 另一种模式只匹配大写: ^[AZ\\d_]+:\\s*\\[([AZ\\d_]+)\\]

You tried ^CMD: \\[ , but your Regex contains Space . 你试过^CMD: \\[ ,但你的Regex包含Space note that in regex you have to use \\s to matching white spaces. 请注意,在正则表达式中,您必须使用\\s来匹配空格。 try your regex but using \\s : 试试你的正则表达式,但使用\\s

if(!VerifyLine(@"^CMD:\s*\[", line))
    ...

explain: 说明:

\s    Matches any white-space character.
*     Matches the previous element zero or more times. 

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

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