Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to create regular expression for {SID} , {MetD}, {car} which should include curly brackets as well.

What I have tried:

Regex r = new Regex(@"^{[A-Za-z]}$");


the above regular expression is not working.
Posted
Updated 7-Aug-22 19:24pm

Your regex specifies:
RegEx
^              Start of line
{              A literal '{'
[A-Za-z]       Any single upper or lower case letter.
}              A literal '}'
$              End of line

You need to add a "multiple times" indicator to that: '*' (zero or more) or '+' (one or more).
Try:
RegEx
^{[A-Za-z]+}$

If you are going to work with Regexes, then get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
 
Share this answer
 
v2
Quote:
the above regular expression is not working.

Try something like
C#
Regex r = new Regex(@"^{[A-Za-z]+}$");

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900