开发者

How could I translate regular expressions in Javascript syntax to .NET syntax

I was taking a look on the jquery.validate plugin source code, to check which expressions are being used to check the validity of a field, so I can implement the same in server side. I got into the Regex for email, for example:

return this.optional(element) ||

/^((([a-z]|\d|[!#\$%&'*+-/=\?\^_{\|}~]|[\u0开发者_JAVA百科0A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+))|((\x22)((((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))(((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?$/i.test(value);

I guess, that the part in bold is the expression, but it doesn't work in .NET (it says the email wrong all the time) I guess it is because the javascript encoded characters. Is there an easy way I can use to translate the expression?

Thanks.


Although it is commercial (i.e. non-free, but cheap) I could not recommend "RegexBuddy" http://www.regexbuddy.com/ highly enough.

Using a standard "standard" RegEx syntax (which you can interactively build and test) it will then generate the source code in correct syntax for use in several environments and many "scenarios" including .net, javascript, Perl, PHP, Python etc.

With my lacklustre knowledge of Regex, this program is a lifesaver.

* disclaimer: No affiliation whatsoever - just a very happy multi-year customer

** Extra note -- I just notice that Jeff Attwood has a testimonial on their homepage!

  • Just for fun: Here is the RFC2822 email verification source generated by RegExBuddy for both .net (C#) and JavaScript

JavaScript:

if (/(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/im.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}

.net C#

try {
    if (Regex.IsMatch(subjectString, @"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|""(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", RegexOptions.IgnoreCase | RegexOptions.Multiline)) {
        // Successful match
    } else {
        // Match attempt failed
    } 
} catch (ArgumentException ex) {
    // Syntax error in the regular expression


I don't know whether it's just an email you are interested in validating or whether it's any arbitrary pattern, but based on what I can see in that example snippet and most other JS regexes, their might not be much differences with the regex specs for .NET, meaning the errors might arise due to some other syntactic differences / errors.

JavaScript's regular expression flavor is part of the ECMA-262 standard for the language (see), though both (Javascript and .NET regex) derive from Perl's regex spec (see). So this might give you clues on where the two are similar and where they differ.

But like I said, if it's just the email pattern you are interested in, try this out:

(\w+@[a-zA-Z_]+?.[a-zA-Z]{2,6})

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜