需要一个正则表达式来匹配三个字符串

时间:2021-01-03 15:22:18

I generally stay away from regular expressions because I seldom find a good use for them. But in this case, I don't think I have choice.

我通常远离正则表达式,因为我很少找到它们。但在这种情况下,我认为我没有选择。

I need a regex for the following situation. I will be looking at three character strings. It will be a match if the first character is 1-9 or the letters o,n,d (lower or upper) AND the second character is 1,2 or 3 and the third character is 0-9.

我需要一个正则表达式来处理以下情况。我将看三个字符串。如果第一个字符是1-9或字母o,n,d(下部或上部)和第二个字符是1,2或3而第三个字符是0-9,则匹配。

Can anybody help me out?

有人可以帮帮我吗?

6 个解决方案

#1


9  

Slight variation on a few other answers. Restrict the input to be exactly the matched text.

其他一些答案略有不同。将输入限制为完全匹配的文本。


^[1-9ondOND][123][0-9]$

#2


5  

[1-9ondOND][123][0-9]

I omitted the ^ and $ (beginning and end of string markers) because you said you'd have three-character strings, but there's no harm in including them, and they may improve speed, not that that'll be a big deal on such short input.

我省略了^和$(字符串标记的开头和结尾)因为你说你有三个字符的字符串,但包含它们没有什么害处,它们可能会提高速度,而不是那么大如此短暂的投入。

Of course, this assumes you're working in a language and locale where the uppercase equivalent of o, n, and d are O, N, and D. If not, you'll need to tell your regex interpreter to ignore case. The mechanism varies by language/framework.

当然,这假设你在一个语言和语言环境中工作,其中o,n和d的大写等价物是O,N和D.如果不是,你需要告诉你的正则表达式解释器忽略大小写。该机制因语言/框架而异。

For python, you'd use something like:

对于python,你会使用类似的东西:

re.match('[1-9ond][123][0-9]', inputstring, re.IGNORECASE)

The re.match forces a match at the beginning of string, so you wouldn't need the ^ in any case.

re.match在字符串的开头强制匹配,因此在任何情况下都不需要^。

#3


2  

Perl /^[1-9ondOND][1-3][0-9]$/

^ = at the start of the string, $ = end of string

^ =在字符串的开头,$ =字符串的结尾

#4


2  

[1-9ond][123][0-9]

and here's a useful place to test out regexes.

这是测试正则表达式的有用位置。

#5


0  

A very late answer, but hope this will help

答案非常晚,但希望这会有所帮助

([1-9]|(?i)(o|n|d))[123][\d]

http://regex101.com/r/vE2jT1/1

#6


-2  

In a PREG-based system (most of them these days):

在基于PREG的系统中(现在大部分都是这样):

^(?:[1-9]|[ond])[1-3][0-9]$

Some systems require the start/end markers (PHP, Perl, but not .NET for instance), if yours does, it'd end up something like:

有些系统需要开始/结束标记(PHP,Perl,但不是.NET),如果有的话,它最终会像:

/^(?:[1-9]|[ond])[1-3][0-9]$/

#1


9  

Slight variation on a few other answers. Restrict the input to be exactly the matched text.

其他一些答案略有不同。将输入限制为完全匹配的文本。


^[1-9ondOND][123][0-9]$

#2


5  

[1-9ondOND][123][0-9]

I omitted the ^ and $ (beginning and end of string markers) because you said you'd have three-character strings, but there's no harm in including them, and they may improve speed, not that that'll be a big deal on such short input.

我省略了^和$(字符串标记的开头和结尾)因为你说你有三个字符的字符串,但包含它们没有什么害处,它们可能会提高速度,而不是那么大如此短暂的投入。

Of course, this assumes you're working in a language and locale where the uppercase equivalent of o, n, and d are O, N, and D. If not, you'll need to tell your regex interpreter to ignore case. The mechanism varies by language/framework.

当然,这假设你在一个语言和语言环境中工作,其中o,n和d的大写等价物是O,N和D.如果不是,你需要告诉你的正则表达式解释器忽略大小写。该机制因语言/框架而异。

For python, you'd use something like:

对于python,你会使用类似的东西:

re.match('[1-9ond][123][0-9]', inputstring, re.IGNORECASE)

The re.match forces a match at the beginning of string, so you wouldn't need the ^ in any case.

re.match在字符串的开头强制匹配,因此在任何情况下都不需要^。

#3


2  

Perl /^[1-9ondOND][1-3][0-9]$/

^ = at the start of the string, $ = end of string

^ =在字符串的开头,$ =字符串的结尾

#4


2  

[1-9ond][123][0-9]

and here's a useful place to test out regexes.

这是测试正则表达式的有用位置。

#5


0  

A very late answer, but hope this will help

答案非常晚,但希望这会有所帮助

([1-9]|(?i)(o|n|d))[123][\d]

http://regex101.com/r/vE2jT1/1

#6


-2  

In a PREG-based system (most of them these days):

在基于PREG的系统中(现在大部分都是这样):

^(?:[1-9]|[ond])[1-3][0-9]$

Some systems require the start/end markers (PHP, Perl, but not .NET for instance), if yours does, it'd end up something like:

有些系统需要开始/结束标记(PHP,Perl,但不是.NET),如果有的话,它最终会像:

/^(?:[1-9]|[ond])[1-3][0-9]$/