如何检查字符串是否有4个连续的字符后没有元音 [英] How to check if string has no vowel after 4 consecutive chars
问题描述
我想检查用户是否输入了接近有效名称的内容。所以如果它输入像 gbcvcvrt
,我想验证它,并将其标记为不是一个真实的名称。类似 theg + vowel
应该被验证为正确的名称。
I want to check if a user is entering something close to a valid name. So if it's entered something like gbcvcvrt
, I would want to validate it and mark this as not a real name. Something like theg+vowel
should be validated as the right name.
在任何长度的字符串,代码应该检测四个连续的非元音字母。
我不希望任何人的名字是节奏的方式
On any length of string the code should detect four consecutive non vowel alphabets. I don't expect anyone's name to be rhythm by the way
考虑
$String = 'therhythm';
我们不能检查前四个字符,我们必须遍历整个字符串并检查四个任何四个连续的非元音字母,而不管它们位于什么字符串上。这样我们可以验证$ String为无效,即使前五个字符将验证它,如果我们检查前四个字母。
We can't check for the first four chars, we have to go through the entire string and check four any four consecutive non-vowel alphabets regardless of where on the string they are situated. That way we can validate $String as not valid even though the first five chars would validate it if we were checking the first four alphabets.
- 不应包含3个连续的元音,
- 不应包含数字,
- 非字母字符
推荐答案
edit:您可以为此使用以下正则表达式:
Based on your latest edit: You can use following regex for this:
^(?!.*?[^aeiou]{5})(?!.*?[aeiou]{3})[a-z]*$
在线演示: http://regex101.com/r/vM0aN9
分拆:
Online Demo: http://regex101.com/r/vM0aN9
Breaking it down:
-
[^ aeiou]
= >匹配任何东西,除了元音之外 -
(?!*。[^ aeiou] {5})
=>没有5个连续辅音的情况 -
(?!。*?[aeiou] {3})
=>没有连续3个元音的情况 -
[az] *
=>确保输入中只有字母 -
[^aeiou]
=> Match anything but vowels(?!.*?[^aeiou]{5})
=> Negative lookahead to make sure there is no case of 5 consecutive consonants(?!.*?[aeiou]{3})
=> To make sure there is no case of 3 consecutive vowels[a-z]*
=> To make sure there are only letters in the input
这篇关于如何检查字符串是否有4个连续的字符后没有元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!