简体   繁体   English

寻找正则表达式

[英]Looking for Regex

I want to validate Winforms text box with regex. 我想用正则表达式验证Winforms文本框。

The input sting example: 输入字符串示例:

ZX1 OR N?V OR 2L? OR ?55 ZX1 OR N?V OR 2L? OR ?55 (any sequence of three symbols length strings with OR between them) ZX1 OR N?V OR 2L? OR ?55 (三个符号长度的字符串的任意序列,它们之间具有或)

What is the regex that you would advise? 您会建议的正则表达式是什么?

UPDATE: Trying this one but seams to be it is not 100% correct 更新:尝试此操作但似乎并非100%正确

string text = "ZX1 OR N?V OR 2L? OR ?55";
Regex r = new Regex("([0-9A-Z?]{3} OR )*[0-9A-Z?]{3}");
"^\\s*\\S{3}(?:\\s+OR\\s+\\S{3})*\\s*$"

should work in a variety of languages. 应该以多种语言工作。

\\S

matches any non-space character, and 匹配任何非空格字符,并且

\\s

matches any space character, so the regex above matches any number of triplets of non-space characters separated by the string "OR" surrounded by space characters. 匹配任何空格字符,因此上面的正则表达式匹配由空格字符包围的字符串"OR"分隔的任意数量的三元组非空格字符。

The ^ and $ serve to ensure that it matches the whole string so you can take those out if you want to find this pattern inside a larger string. ^$用于确保它与整个字符串匹配,因此,如果您想在较大的字符串中找到此模式,可以将其删除。

What is the list of possible symbols you can have? 您可以使用的可能符号列表是什么? can you have at most one question mark? 您最多可以有一个问号吗? This will match what you've given, but it will also match multiple question marks. 这将与您所给的相匹配,但也将与多个问号相匹配。

([A-Z?]{3} OR )*[A-Z?]{3}

尝试...

(([\w\S]{3}\s+)or\s+)+[\w\S]{3}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM