0

I am tasked to use regex to verify a pattern in a string instead of using substrings due to accuracy reasons.

The string is as follows:

I need to verify that the string is in the format as [GMT-CASE_<6 digits>].


A sample string:

[GMT-CASE_000001] Query on...

May I know how can this be done?

Thank you.

2
  • Do you mean matching 6 digits? \[GMT-CASE_[0-9]{6}] Commented Sep 21, 2019 at 12:27
  • @Thefourthbird yes, matching 6 digits. my apologies. Commented Sep 21, 2019 at 12:29

3 Answers 3

1

You can use

\[GMT-CASE_[0-9]{6}\]

Explanation:

\[GMT-CASE_

This starts with the literal [GMT-CASE_. Since a square bracket has a special meaning in regular expressions you need to escape it with a backslash.

[0-9]{6}

Any character between 0 and 9, repeated 6 times.

\]

The closing bracket, again needed to be escaped.

Alternatively you can use \d instead of [0-9] for the digits. This would match non latin digits like the arabic ٦ as well.

Sign up to request clarification or add additional context in comments.

Comments

1

One possible answer would be \[GMT-CASE_[0-9]{6}\]

Check this https://regex101.com/r/tuDioP/1

Comments

0

try to use the @ before the string and the backslash before the brackets

Regex rx = new Regex(@"\[GMT-CASE_[0-9]{6}\]");
Console.WriteLine(rx.IsMatch("[GMT-CASE_000001]"));

; Use @ before to escape the \ behavior also if u want just only one match its important to mention the begin and the end of the regex ^..$

Regex rx = new Regex(@"^\[GMT-CASE_[0-9]{6}\]$");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.