Nick Sinas
Nick Sinas

Reputation: 2634

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.
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.

Upvotes: 0

Views: 4367

Answers (3)

Ria
Ria

Reputation: 10347

You tried ^CMD: \[, but your Regex contains Space. note that in regex you have to use \s to matching white spaces. try your regex but using \s:

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

explain:

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

Upvotes: 0

godspeedlee
godspeedlee

Reputation: 672

try with this: ^\w+:\s*\[(\w+)\], \w will match alphabet, digit, and underscore
and another pattern will match upper case only: ^[A-Z\d_]+:\s*\[([A-Z\d_]+)\]

Upvotes: 0

Wug
Wug

Reputation: 13196

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.

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:

^([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 _

Upvotes: 2

Related Questions