开发者

Regular Expression; Find whether a line contains any word with more than X characters

开发者 https://www.devze.com 2023-01-02 06:52 出处:网络
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.

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 ma开发者_如何学Pythonrgins 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


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

  • regular-expressions.info/Lookarounds


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.

0

精彩评论

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