我尝试使用正则表达式来匹配不包含小于号 (<) 后跟非空格的字符序列的字符串。以下是一些示例
有效 - “新描述。”
有效 - “新描述。”
无效 - “新描述。”
我似乎找不到正确的表达式来获得匹配。我正在使用 Microsoft 正则表达式验证器,因此我需要它是匹配项,而不是使用代码来否定匹配项。
任何帮助将不胜感激。
谢谢,
戴尔
I'm trying to use regular expressions to match a string that does not contain the sequence of characters of a less than symbol (<) followed by a non space. Here are some examples
Valid - "A new description."
Valid - "A < new description."
Invalid - "A <new description."
I can't seem to find the right expression to get a match. I'm using the Microsoft Regular Expression validator, so I need it to be a match and not use code to negate the match.
Any assistance would be appreciated.
Thanks,
Dale
发布评论
评论(5)
如果字符串中的最后一个字符是“<”,则对空格进行负向前瞻可以使其匹配。这是另一种方法:
先行扫描整个字符串以查找“<”紧接着是一个非空白字符。如果找不到,则“.+”继续并匹配该字符串。
Doing a negative lookahead for space allows it to match if the last character in the string is "<". Here's another way:
The lookahead scans the whole string for a "<" immediately followed by a non-whitespace character. If it doesn't find one, the ".+" goes ahead and matches the string.
换句话说,您的字符串中允许出现两种情况:
<
之外的任何字符<
后跟一个空格。我们可以直接将其写为:
In other words, you allow two things in your string:
<
<
followed by a space.We can write that quite directly as:
使用否定的前瞻:“<(?!)”
Use a negative look-ahead: "<(?! )"
我想这可能就是您所寻找的。
这会查找带有小于号并且后面缺少空格的内容。
在这种情况下,它将匹配
"
但不确定您希望它匹配多少。
I think this might be what your looking for.
This looks for something that has a less then sign and has a space missing after it.
In this case it would match
"<n"
Not sure how much it you want it to match though.