a quick hand would be much appreciated.
I’m trying to make a small algorithm to input the text from a text box into a regex expression. As follows:
string bar = String.Format(@"^{0}*(q*x|x*q)*", foo.Text);
the regex class is passed the bar variable above and initialized when the event is fired. However, it only returns hits when there is repeating text. E.G bb in the textbox returns all results that begin with b, and 1b returns matches on every word.
??? is there something wrong with my regex or my logic? (or both! :P)
EDIT i am attempting to match a list of strings loaded from a text file. all words that contain both a q and an x at least once, beginning with the letters in the text box. examples:
arquifoux
benzofuroquinoxaline
benzoquinoxaline
disquixote
equiaxe
I think you want
The
.matches any character.The question mark in
.*?just makes the match non-greedy (ie the ‘q’ will match the first q it finds), although in your case they’re not really necessary.Your previous, ‘q*’, matches “0 or more q characters”.
You could further improve your regex:
Which only matches lowercase letters. (you could feed in the case insensitive flag to your regex to have it match any case).