views:

121

answers:

2

Hi,

I am trying to use a Validator on a ASP.NET site and need to find whether the Street Address textbox contains a valid entry.

Entries with words that are longer than X characters (in this case 25, with no punctuation or spaces) will cause the HTML on a printed A4 page to not wrap properly and therefore not to confrom to certain sizes correctly pushing the margins off.

For a street address I want to match that something like "201 Long Road" is valid but "235 ReallyLongAndNarrowWindingRoadBesideTheRiver Street" is invalid.

Using a Microsoft .Net Regular Expression Validator I need to know what the RegEx pattern might be.

I think if it does find a match the Validator will fire correctly however if there is no match the Validator won't fire and the Update button (in this case) won't fire.

Since Street addresses can contain Capital Letters and numbers etc. it will need to accomodate for that and also Spaces, Commas, Semi-Colons and Colons and Hyphens are valid characters too.

Any help would be greatly appreciated as I am really stuck with this problem.

Thanks, David

+1  A: 

You can use the following.

"^\w{0,25}(?>\W+\w{1,25})*\W*$"

It starts with 0-25 word characters. We then have a repeating group of least one non-word, then 1-25 word. Finally, we can have 0 or more non-word at the end. The ?> is my untested attempt to eliminate exponential backtracking.

Matthew Flaschen
Hi Matthew,Thanks for the quick reply however I have tried this.The problem with the Microsoft Validators is that they need to match the condition to be valid and allow the Update etc. to complete.If I was to use this I would need to have words with more than 26 characters. What I want is the opposite in that I want the line to contain words without 25 characters or more.It's basically the inverse of what you posted but I can't seem to find the correct Expression for this.David
Simpsoid
@Simpsoid make sure all words match `\w{1,25}` By the way `\w` allows underscores too.
Amarghosh
@Amargorsh: This becomes invalid with spaces which Street Addresses have as posted in my original example.
Simpsoid
@Matthew Flaschen: Wow! Thanks very much this is exactly what I needed. The untested part works prefectly with our scenario anyway.Thank you very much I really appreciate it! David
Simpsoid
I think you'll slightly better performance by changing the second 0 to the 1.
Matthew Flaschen
+1  A: 

Use negative assertion:

(?!.*\S{25})

\S{25} matches a sequence of 25 \S (which must be a subsequence of anything longer). (?!pattern) is a negative lookahead, an assertion that is true if the pattern doesn't match. .* allows the lookahead to look as far as necessary.

The whole pattern therefore is

^(?!.*\S{25}).*$

This matches all string that do NOT contain \S{25} (see it in action on rubular.com, with threshold 10).

See also

polygenelubricants
Thanks @polygenelubricants. While it should be noted both examples work in my scenario I would think from looking at the examples provided here that the one above is more efficient. Thank you for this. I will be using this one in our Validator.
Simpsoid