匹配不包含特定字符序列的字符串

发布于 2024-08-03 05:30:22 字数 262 浏览 2 评论 0 原文

我尝试使用正则表达式来匹配不包含小于号 (<) 后跟非空格的字符序列的字符串。以下是一些示例

有效 - “新描述。”
有效 - “新描述。”
无效 - “新描述。”

我似乎找不到正确的表达式来获得匹配。我正在使用 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒绊 2024-08-10 05:30:22
@"^(?:[^<]+|<(?!\s))*$"

如果字符串中的最后一个字符是“<”,则对空格进行负向前瞻可以使其匹配。这是另一种方法:

^(?!.*<\S).+$

先行扫描整个字符串以查找“<”紧接着是一个非空白字符。如果找不到,则“.+”继续并匹配该字符串。

@"^(?:[^<]+|<(?!\s))*$"

Doing a negative lookahead for space allows it to match if the last character in the string is "<". Here's another way:

^(?!.*<\S).+$

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.

伴我心暖 2024-08-10 05:30:22

换句话说,您的字符串中允许出现两种情况:

  1. 除了 < 之外的任何字符
  2. < 后跟一个空格。

我们可以直接将其写为:

/([^<]|(< ))+/

In other words, you allow two things in your string:

  1. Any character EXCEPT a <
  2. A < followed by a space.

We can write that quite directly as:

/([^<]|(< ))+/
网白 2024-08-10 05:30:22

使用否定的前瞻:“<(?!)”

Use a negative look-ahead: "<(?! )"

薄荷梦 2024-08-10 05:30:22

我想这可能就是您所寻找的。

Valid - "A new description."
Valid - "A < new description."
Invalid - "A <new description."



 Try this:   <\S

这会查找带有小于号并且后面缺少空格的内容。

在这种情况下,它将匹配 "

但不确定您希望它匹配多少。

I think this might be what your looking for.

Valid - "A new description."
Valid - "A < new description."
Invalid - "A <new description."



 Try this:   <\S

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.

咿呀咿呀哟 2024-08-10 05:30:22
var reg = new Regex(@"<(?!\s)");
string text = "it <matches";
string text2 = "it< doesn't match";

reg.Match(text);// <-- Match.Sucess == true
reg.Match(text2);// <-- Match.Sucess == false
var reg = new Regex(@"<(?!\s)");
string text = "it <matches";
string text2 = "it< doesn't match";

reg.Match(text);// <-- Match.Sucess == true
reg.Match(text2);// <-- Match.Sucess == false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文