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

查看:22
本文介绍了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  "

但不匹配(即这些是合法的)

But does NOT match (i.e. these are legal)

"North West"
"NorthWest"

不幸的是,我的正则表达式似乎以错误的方式匹配,我了解到正则表达式中没有非"运算符.此外,我当前的表达式与有效字符串 "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 个字符串,您可以使用环视 ^(?=[^ ]).*(?<=[^ ])$.参见 another demo 并注意 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天全站免登陆