正则表达式模式不匹配输入字符串

时间:2022-09-13 07:48:45

I need to find a pattern that restricts the input of data. What I need it to restrict input to:

我需要找到一种限制数据输入的模式。我需要它来限制输入:

First character must be "S" Second character must be "S", "T", "W", "X" or "V" The next 6 must be numbers for 0 - 9 The last 2 can be any capital letter or any number 0 - 9

第一个字符必须是“S”第二个字符必须是“S”,“T”,“W”,“X”或“V”接下来的6个必须是0到9的数字。最后2个可以是任何大写字母或任何数字0 - 9

So my research led me to put this together.....

所以我的研究让我把它放在一起......

^[S][STWXV]/d{6}[A-Z0-9]{2}$

From what I read:

从我读到的:

[S] mean the capital letter S only [STWXV] means any one letter from this list /d{6} means 6 digits [A-Z0-9]{2} means any 2 characters A - Z or 0 - 9

[S]表示仅大写字母S [STWXV]表示此列表中的任何一个字母/ d {6}表示6位[A-Z0-9] {2}表示任意2个字符A - Z或0 - 9

I am not looking for a match anywhere in a string, i need the whole string to match this pattern.

我不是在字符串中的任何地方寻找匹配,我需要整个字符串来匹配这个模式。

So why does Regex.IsMatch("SX25536101", "^[S][STWXV]/d{6}[A-Z0-9]{2}$") return false?

那么为什么Regex.IsMatch(“SX25536101”,“^ [S] [STWXV] / d {6} [A-Z0-9] {2} $”)会返回false?

Ive obviously gone wrong somewhere but this is my first attempt at Regular Expressions and this makes no sense :(

我在某处显然出了问题,但这是我对正则表达式的第一次尝试,这没有任何意义:(

2 个解决方案

#1


8  

You need to use \d for digits:

您需要使用\ d表示数字:

 Regex.IsMatch("SX25536101", @"^[S][STWXV]\d{6}[A-Z0-9]{2}$"); // true

NOTE: Here is nice Regular Expressions Cheat Sheet which can help you in future.

注意:这是很好的正则表达式备忘单,可以帮助您将来。

#2


-1  

Or don't use \d at all (preferred and faster):

或者根本不使用\ d(首选且更快):

^S[STWXV][0-9]{6}[A-Z0-9]{2}$

See here or here for why.

请参阅此处或此处了解原因。

#1


8  

You need to use \d for digits:

您需要使用\ d表示数字:

 Regex.IsMatch("SX25536101", @"^[S][STWXV]\d{6}[A-Z0-9]{2}$"); // true

NOTE: Here is nice Regular Expressions Cheat Sheet which can help you in future.

注意:这是很好的正则表达式备忘单,可以帮助您将来。

#2


-1  

Or don't use \d at all (preferred and faster):

或者根本不使用\ d(首选且更快):

^S[STWXV][0-9]{6}[A-Z0-9]{2}$

See here or here for why.

请参阅此处或此处了解原因。