User.1
User.1

Reputation: 2642

Allowing Asterisks And Question Marks In A C# RegEx pattern

QUESTION: Where do I put the * or \* or \\* in a C# RegEx pattern to match asterisks in a target ?

Ditto for \? or \\?

Details...

I have an app which allows this kind of input

Shorthand description...

0, through F, as well as 00, through FF, ignoring the case of the letters. Optional whitespace before and after are okay. The comma is required for delimiting, but not allowed on the end. (last char must be whitespace or end-of-text)

After that, the pattern can repeat as often as the user wants. (Whitespace does not have to balance.)

Example...

 00, F1, 33, A7,    21, 14, 0D, 0A, 30,30,30,31, 41,    3e,3e,3e,  41, 5A,   0 

The following code works perfectly for this...

 private bool See_If_This_Input_Is_Okay(string The_Callers_Text)
    {
        String pattern = @"^\s*([0-9A-Fa-f]{1,2},\s*)*[0-9A-Fa-f]{1,2}\s*$";

        bool matches = Regex.IsMatch(The_Callers_Text, pattern);

        return (matches);
    }

Now I want the pattern to accommodate...

Shorthand description...

0, through F, or 00, through FF, or *, or **, or ?, or ??,

(again with zero-to-random whitespace before and after [but not within] each string, commas enforced)

Example...

00, ??, 33, A7,    21, 14, ?,?, 30,30,30,31, **,    3e,3e,3e,  *, 5A,   0 

After reading 20 or 30 similar posts here, I still cannot figure out where to place the \* and the \? sequence in the pattern string.

I also tried \\* and got nowhere.

That includes after the f, before the $, after the s* (either one) inside the brackets, outside the brackets; I suppose I could program my text editor to make fifty one copies of the line in question and try each position; but I really want to understand this, not just make it happen.

If anyone can explain what goes where, thanks, that will do. What I would really like to know is where this is explained and documented. I will leave out the dozen webpages I have visited which purport to explain this.

Thank you to anyone who can answer this.

Upvotes: 2

Views: 186

Answers (2)

Jerry
Jerry

Reputation: 71558

Well, first, you could make the regex a little faster by changing the grouping a bit to:

@"^\s*[0-9A-Fa-f]{1,2}(,\s*[0-9A-Fa-f]{1,2}\s*)*$"

Now if you want to allow * and ? as well, you could use something like this:

@"^\s*([0-9A-Fa-f]{1,2}|\*\*?|\?\??)(,\s*([0-9A-Fa-f]{1,2}|\*\*?|\?\??)\s*)*$"

I basically changed [0-9A-Fa-f]{1,2} to ([0-9A-Fa-f]{1,2}|\*\*?|\?\??).

You might also use RegexOptions.IgnoreCase instead of using A-Fa-f:

String pattern = @"^\s*([0-9A-F]{1,2}|\*\*?|\?\??)(,\s*([0-9A-F]{1,2}|\*\*?|\?\??)\s*)*$";
bool matches = Regex.IsMatch(The_Callers_Text, pattern, RegexOptions.IgnoreCase);

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39375

You have two character class [] in your regex. Just place * and ? character inside both the character class and it should work for you.

^\s*([0-9A-Fa-f*?]{1,2},\s*)*[0-9A-Fa-f*?]{1,2}\s*$
               ^^                      ^^

Upvotes: 3

Related Questions