PostSharp参数验证-使用RegularExpressionAttribute查找前导/尾随空格 [英] PostSharp Parameter Validation - Using RegularExpressionAttribute to find leading/trailing spaces

查看:97
本文介绍了PostSharp参数验证-使用RegularExpressionAttribute查找前导/尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PostSharp 3.1通过验证属性来验证属性的参数.我想使用 RegularExpressionAttribute 进行验证,该验证采用代表正则表达式的字符串.如果字符串具有任何前导或尾随空格,我想抛出一个异常,但是该字符串可能在单词之间包含空格.

I'm using PostSharp 3.1 to validate parameters of properties using validation attributes. I would like to use RegularExpressionAttribute to perform the validation, which takes in a string representing the regex. I would like to throw an exception if the string has any leading or trailing whitespace, but the string may contain spaces between words.

在使用PostSharp属性之前,我进行了如下检查:

Prior to using PostSharp attributes, I performed a check like this:

if(name == name.Trim())
{
    throw new ArgumentException("name", "Name contains leading/trailing whitespace");
}

相反,我想要这样的东西:

Instead, I want something like this:

[RegularExpression("[ \\s]+|[ \\s]+$")]
public name { get; private set; }

哪个匹配项(即这些匹配项是非法的并引发异常)

Which matches (i.e. these are illegal and throw exceptions)

"  North West"
"North West  "
"  North West  "
"  NorthWest"
"NorthWest  "
"  NorthWest  "

但不匹配(即合法)

"North West"
"NorthWest"

不幸的是,看来我的正则表达式匹配错误的方式,并且让我明白了正则表达式中没有"not"运算符.另外,我当前的表达式与有效字符串"North West"匹配(并引发异常),因为它与中间的空格匹配.

Unfortunately it seems my regex matches the wrong way around and I am given to understand there is no "not" operator in regex. Also, my current expression matches (and throws exception) on the valid string "North West" as it matches the space in the middle.

是否可以在不创建自定义属性的情况下做到这一点?

Is it possible and neat to do this without creating a custom attribute?

推荐答案

RegularExpressionAttribute中的正则表达式必须与整个文本匹配.这是源代码的摘录:

The regex inside RegularExpressionAttribute must match the whole text. Here is an excerpt from the source code:

override bool IsValid(object value) {
 //...
 // We are looking for an exact match, not just a search hit. This matches what
 // the RegularExpressionValidator control does
 return (m.Success && m.Index == 0 && m.Length == stringValue.Length);

因此,您需要添加.*来捕获中间的任何内容.

So, you need to add .* to capture anything that is in-between.

您可以使用

^[^ ].*[^ ]$

正则表达式的意思是匹配一个非空格,然后匹配除空格以外的任意数量的字符,并在末尾匹配非空格".这也意味着必须至少有2个字符匹配. 这里是一个演示,您可以在其中测试此正则表达式.尽管它是用于PCRE的,但是该模式在C#环境中的行为相同(只是出于演示目的,我添加了m标志).

The regex means "match a non-space, then any number of characters other than whitespace, and non-space at the end". It also means there must be at least 2 characters to match. Here is a demo where you can test this regex. Though it is for PCRE, the pattern will behave the same in C# environment (just I added m flag for demonstration purposes).

为了只执行检查并允许甚至1或0个字符串,您可以使用环视^(?=[^ ]).*(?<=[^ ])$.请参阅另一个演示,并注意最后一行,其中1现在被视为有效输入.

In order to just perform the check and allow even 1 or 0 character string, you can use look-arounds ^(?=[^ ]).*(?<=[^ ])$. See another demo and pay attention to the last line where 1 is now considered a valid input.

这篇关于PostSharp参数验证-使用RegularExpressionAttribute查找前导/尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆