samedi 5 avril 2014

Permettant les astérisques et points d'interrogation dans A c# RegEx motif - Stack Overflow


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



  • One or two hex chars: 0-9, or a-f, or A-F

  • Then a mandatory comma: ,

  • Then optional white space (as much or as little as desired, or none):


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



  • One or two adjacent asterisks, either: * or **, Then a mandatory comma: ,

  • One or two adjacent question marks, either: ? or ??, Then a mandatory comma: ,


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.




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*$
^^ ^^



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);


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



  • One or two hex chars: 0-9, or a-f, or A-F

  • Then a mandatory comma: ,

  • Then optional white space (as much or as little as desired, or none):


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



  • One or two adjacent asterisks, either: * or **, Then a mandatory comma: ,

  • One or two adjacent question marks, either: ? or ??, Then a mandatory comma: ,


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.



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*$
^^ ^^


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);

0 commentaires:

Enregistrer un commentaire