2011年11月10日星期四

A Question of Regular Expressions

Can you think of a better solution?I don't pretend to moncler ski blouson red jacket be an expert on regular expressions although there are a couple of articles here that introduce them for you. This mini-tutorial, for example, shows how to use them in ASP.NET: Using Regular Expressions in ASP.NETIn brief, a regular expression is a string of text that will either "match" or "not match" patterns in other strings. Most people have at least seen a DOS command (or even a search) where you can find executable programs using a search argument: "*.exe". That means find all files with any name (that's what the "*" means) that end in the characters ".exe". A regular expression is the same thing with an advanced degree and a job at the bank."Sheela" wrote to ask a question about this regular expression: moncler ski blouson deep blue jacket Dim mRegExp As Regex mRegExp New RegexValid expressions are a number, or a decimal, or a number or a decimal strung together with other numbers or decimals with a "+" sign between them." For example, "2+2" would be a valid match. The problem was that expressions like "2+2+" were being matched as well.The way it works is -- the string must start with a number between 1 and 9[0-9]* -- the next character must be a number between 0 and 9 and it can be repeated-- the next group of characters is optional, but if it exists, then it must start with a "." and be followed by a series of numbers between 0 and 9 -- the group of characters described below repeated+ -- a plus signand -- the same moncler clairy jacket pink characters described earlierThe problem is that regular expressions are "eager". That is, when they match a string, they quit with a "match" result no matter what else might be there. So "2+2", "2+2+" and "2GeorgeWashington" all match. This was a problem because the purpose of the regular expression was to validate input typed into a TextBox and it didn't do that very well.The solution I recommended was to extract the match from the TextBox input and use that instead.Dim matchedString As StringmatchedString = Regex.Match(txtEntered.Text,.ToStringDebug.WriteLine(matchedString)But ... like I said ... I don't pretend to be a Regex guru. Does anyone else have a better idea?(The complete cartoon can be seen at: XKCD: A webcomic of romance,sarcasm, math, and language.)

没有评论:

发表评论