在正则表达式中设置最小和最大字符?

时间:2022-02-12 07:22:31

I've written a regular expression that matches any number of letters with any number of single spaces between the letters. I would like that regular expression to also enforce a min and max number of characters, but I'm not sure how to do that (or if it's possible).

我写了一个正则表达式,它匹配任意数量的字母以及任意数量的单个空格。我希望正则表达式也强制执行最小和最大字符数,但我不确定如何执行(如果可能的话)。

My regular expression is:

我的正则表达式是:

[A-Za-z](\s?[A-Za-z])+

I realized it was only matching two sets of letters surrounding a single space, so I modified it slightly to fix that. The original question is still the same though.

我意识到它只匹配了一个空格周围的两组字母,所以我稍微修改了一下。最初的问题仍然是一样的。

Is there a way to enforce a minimum of three characters and a maximum of 30?

是否有一种方法可以强制执行最少三个字符和最多30个字符?

4 个解决方案

#1


30  

Yes

是的

Just like + means one or more you can use {3,30} to match between 3 and 30

正如+表示可以使用{3,30}匹配3到30之间的一个或多个

For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters

例如[a-z]{3,30}匹配3到30个小写字母

From the documentation of the Pattern class

来自Pattern类的文档

X{n,m}    X, at least n but not more than m times

In your case, matching 3-30 letters followed by spaces could be accomplished with:

在你的例子中,3-30个字母和空格的匹配可以通过以下方式完成:

([a-zA-Z]\s){3,30}

If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)

如果需要拖尾空格,可以使用:(2-29乘以字母+空格,然后是字母)

([a-zA-Z]\s){2,29}[a-zA-Z]

If you'd like whitespaces to count as characters you need to divide that number by 2 to get

如果你想用空格作为字符,你需要将这个数字除以2。

([a-zA-Z]\s){1,14}[a-zA-Z]

You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet

您可以添加\ s ?如果末尾的空格是可选的,则对应最后一个空格。这些都是在RegexPlanet上测试的

If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations

如果你想整个字符串完全是3至30个字符可以使用超前添加(? = ^。{ 3 30 } $)初的RegExp和删除其他大小限制

All that said, in all honestly I'd probably just test the String's .length property. It's more readable.

老实说,我可能只是测试了字符串的。length属性。它是更具可读性。

#2


5  

This is what you are looking for

这就是你要找的

^[a-zA-Z](\s?[a-zA-Z]){2,29}$

^ is the start of string

^是字符串的开始

$ is the end of string

$是字符串的末尾

(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..

(\ s ?[a-zA-Z]){ 2,29 }将匹配(\ s ?[a-zA-Z])2 - 29倍. .

#3


3  

Actually Benjamin's answer will lead to the complete solution to the OP's question. Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.

实际上本杰明的回答将会引出OP问题的完整答案。使用lookaheads可以限制字符的总数,并将匹配限制为一组字母和(可选的)单个空格。

The regex that solves the entire problem would become

解决整个问题的regex将变成

(?=^.{3,30}$)^([A-Za-z][\s]?)+$

This will match AAA, A A and also fail to match AA A since there are two consecutive spaces. I tested this at http://regexpal.com/ and it does the trick.

这将会匹配AAA, A,也不能匹配AAA,因为有两个连续的空格。我在http://regexpal.com/中测试了这个功能,它可以做到这一点。

#4


0  

you should use

你应该使用

[a-zA-Z ]{20}

[for allowed characters]{for limit of number of characters}

[for allowed characters]{for limit of characters}

#1


30  

Yes

是的

Just like + means one or more you can use {3,30} to match between 3 and 30

正如+表示可以使用{3,30}匹配3到30之间的一个或多个

For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters

例如[a-z]{3,30}匹配3到30个小写字母

From the documentation of the Pattern class

来自Pattern类的文档

X{n,m}    X, at least n but not more than m times

In your case, matching 3-30 letters followed by spaces could be accomplished with:

在你的例子中,3-30个字母和空格的匹配可以通过以下方式完成:

([a-zA-Z]\s){3,30}

If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)

如果需要拖尾空格,可以使用:(2-29乘以字母+空格,然后是字母)

([a-zA-Z]\s){2,29}[a-zA-Z]

If you'd like whitespaces to count as characters you need to divide that number by 2 to get

如果你想用空格作为字符,你需要将这个数字除以2。

([a-zA-Z]\s){1,14}[a-zA-Z]

You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet

您可以添加\ s ?如果末尾的空格是可选的,则对应最后一个空格。这些都是在RegexPlanet上测试的

If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations

如果你想整个字符串完全是3至30个字符可以使用超前添加(? = ^。{ 3 30 } $)初的RegExp和删除其他大小限制

All that said, in all honestly I'd probably just test the String's .length property. It's more readable.

老实说,我可能只是测试了字符串的。length属性。它是更具可读性。

#2


5  

This is what you are looking for

这就是你要找的

^[a-zA-Z](\s?[a-zA-Z]){2,29}$

^ is the start of string

^是字符串的开始

$ is the end of string

$是字符串的末尾

(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..

(\ s ?[a-zA-Z]){ 2,29 }将匹配(\ s ?[a-zA-Z])2 - 29倍. .

#3


3  

Actually Benjamin's answer will lead to the complete solution to the OP's question. Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.

实际上本杰明的回答将会引出OP问题的完整答案。使用lookaheads可以限制字符的总数,并将匹配限制为一组字母和(可选的)单个空格。

The regex that solves the entire problem would become

解决整个问题的regex将变成

(?=^.{3,30}$)^([A-Za-z][\s]?)+$

This will match AAA, A A and also fail to match AA A since there are two consecutive spaces. I tested this at http://regexpal.com/ and it does the trick.

这将会匹配AAA, A,也不能匹配AAA,因为有两个连续的空格。我在http://regexpal.com/中测试了这个功能,它可以做到这一点。

#4


0  

you should use

你应该使用

[a-zA-Z ]{20}

[for allowed characters]{for limit of number of characters}

[for allowed characters]{for limit of characters}