About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).
Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

Thursday, October 25, 2007

Optimize your regular expressions

An interesting article by Tess: http://blogs.msdn.com/tess/archive/2007/10/25/net-hang-case-study-high-cpu-because-of-poorly-formatted-regular-expressions-identifying-tight-loops.aspx

She went into the issue that if you don't optimize your regular expressions, the run time can be increase exponentially as the size of the string increases.

The reason for this, is because the regular expression is a NFA (Non-Deterministic Finite Automata)

Her example:
given the string: helloworldhelloworld
using regex: ([a-z]+)*= versus [a-z]*

given the string: abcde
Regexp: (abe|abc)(dg|dc|de) versus ab(e|c)d(g|c|e)

The long version

The order of execution here will be something like

a matches a in abe
b matches b in abe
c doesn't match e in abe, backtrack to try with the next expression
c matches c in abc - done with first part
d matches d in dg
e doesn't match g in dg, backtrack to try with the next expression
e doesn't match c in dc, backtrack to try with the next expression
e matches e in de - done with the regular expression


She recommends to avoid/decrease


  • nested quantifiers (*, +, {m,n})

  • backtracks

  • ambiguous matches (i.e. in this case the number of combinations that cause a positive match for part of the string)

Thursday, September 20, 2007

Regular Expression App

I made a quick and simple app that would allow me to open a file and run regular expressions on to it. I also implmented a replacement, so if you wanted to run replacements on the file it would be easily done.

http://williamandrus.tripod.com/RegEx.html

Monday, September 25, 2006

MS ASP.Net Assessment Question: Validate Email Address

You create a Web Form that enables users to confirm their e-mail addresses.

You need to validate that users type e-mail addresses on the Web Form and that the e-mail addresses exist in a database.

What should you do?

A) Configure a RequiredFieldValidator control, a RegularExpressionValidator control, and a CustomValidator control.
B) Configure a RegularExpressionValidator control.
C) Configure a CompareValidator control.
D) Configure a RequiredFieldValidator control and a RegularExpressionValidator control.

A)Best Answer, by having the controls use for checking for populating textboxes and valid email address, then you will have to use the custom validator to check if both textboxes are also similar.
B)Wrong, since the regular expression validator doesn't check unless data has been entered into the textbox.
C)Wrong, only compares two textboxes, won't check if they are correct.
D) Wrong, would check for populate field and correct format for email, but not if two textboxes are similar.