【问题标题】:How to check if string has no vowel after 4 consecutive chars [closed]如何在连续 4 个字符后检查字符串是否没有元音 [关闭]
【发布时间】:2014-01-18 04:43:41
【问题描述】:

我想检查用户是否输入了接近有效名称的内容。因此,如果输入了gbcvcvrt 之类的内容,我想对其进行验证并将其标记为非真实姓名。 theg+vowel 之类的东西应该被验证为正确的名称。

在任何长度的字符串上,代码都应该检测到四个连续的非元音字母。 顺便说一句,我不希望任何人的名字是节奏

考虑

$String = 'therhythm';

我们无法检查前四个字符,我们必须遍历整个字符串并检查四个任意四个连续的非元音字母,无论它们位于字符串的哪个位置。这样我们可以验证 $String 无效,即使如果我们检查前四个字母,前五个字符也会验证它。

  • 也不应包含 3 个连续的元音,
  • 不应包含数字,
  • 非字母字符

【问题讨论】:

  • 所以gbcvacvrt 有效吗?元音如何验证名称?很难区分什么是“有效”和什么不是,除非你在后面使用某种字典
  • 我很高兴我的典型 asdfasdf 通过
  • Merckx 和 Schnell 是实际姓氏。 Bauer 和 Aoe 同上。这只是我的想法;实际研究应该会带来更多反例。
  • 这个问题似乎离题了,因为试图以任何方式验证人们的名字听起来都是个糟糕的主意。
  • @deceze +1;另见kalzumeus.com/2010/06/17/…

标签: php regex char preg-replace preg-match


【解决方案1】:

根据您的最新编辑:您可以为此使用以下正则表达式:

^(?!.*?[^aeiou]{5})(?!.*?[aeiou]{3})[a-z]*$

在线演示:http://regex101.com/r/vM0aN9

分解:

  • [^aeiou] => 匹配除元音以外的任何内容
  • (?!.*?[^aeiou]{5}) => 负前瞻以确保不存在 5 个连续辅音的情况
  • (?!.*?[aeiou]{3}) => 确保没有连续 3 个元音的情况
  • [a-z]* => 确保输入中只有字母

【讨论】:

  • 有一些解释会更好。
  • 当然我会为我的问题添加一些解释。
  • 在辅音连续的情况下,它将匹配testteeee与超过3个连续的etestttttttt
  • 好的,现在检查更新。
【解决方案2】:

对于您的测试字符串asdfasdf,请使用以下正则表达式:

([bcdfghjklmnpqrstvwxyz]{3}+)/ig

Online Example

它会捕捉到 3 个或更多重复的辅音,你可以用崇高的逻辑对其进行测试!

使用 PHP 你可以尝试这样的事情:

function testString($data) {
    if (!preg_match('/([bcdfghjklmnpqrstvwxyz]{3}+)/', $data)) {
        return 'Valid';
    } else {
        return 'Invalid';
    }
}
echo testString('asasacdDdfasdf');
echo '<br>';
echo testString('Ilia');

Online Example

【讨论】:

    【解决方案3】:
    ^(?![aeiou]{3,})(?:\D(?![^aeiou]{4,}[aeiou]*)(?![aeiou]{4,})){3,}$
    

    这个正则表达式有你所有的条件:

    <?php
        $values = array(
                "test",
                "al",
                "beautiful",
                "eautiful",
                "mohsen",
                "ehsasssss",
                "a test",
                "yeeeeee",
                "test",
                "mohammad",
                "ali",
                "mohsen",
                "mohsenmlpl",
                "allllll",
                "allilllllo",
                "thisisatest",
                "zagros",
                "4o54o45646o",
                "therhythm",
                "theg+vowel",
                "gbcvcvrt",
                "user3109875",
            );
        foreach ($values as $key => $value)
        {
            echo "$value => ", preg_match("/^(?![aeiou]{3,})(?:\D(?![^aeiou]{4,}[aeiou]*)(?![aeiou]{4,})){3,}$/", $value)? "TRUE" : "FALSE", "\n";
        }
    

    输出

    test => TRUE
    al => FALSE
    beautiful => TRUE
    eautiful => FALSE
    mohsen => TRUE
    ehsasssss => FALSE
    a test => TRUE
    yeeeeee => FALSE
    test => TRUE
    mohammad => TRUE
    ali => TRUE
    mohsen => TRUE
    mohsenmlpl => FALSE
    allllll => FALSE
    allilllllo => FALSE
    thisisatest => TRUE
    zagros => TRUE
    4o54o45646o => FALSE
    therhythm => FALSE
    theg+vowel => TRUE
    gbcvcvrt => FALSE
    user3109875 => FALSE
    

    【讨论】:

    • 这将匹配:$)+abc
    • @M42 是的,应该。 OP 也需要非字母数字字符
    【解决方案4】:

    试试这个

    <?php
    
    $string = "gkwohtfeiak3d";
    
    function checkstring($input){
    
        if(preg_match("/[0-9]+/", $input, $match) || preg_match("/[AEIOU]{3,}/i", $input, $match)){
    
                if(preg_match("/[0-9]+/", $input, $match)){
                        echo "Your string has numbers.<br/>";
                }
    
                if(preg_match("/[AEIOU]{3,}/i", $input, $match)){
                        echo "Your string has three or more consecutive vowels";
                }
    
        }else{
    
            $checkthis = substr($input, 4);
    
            if(preg_match("/[AEIOU]+/i", $checkthis, $match2)){
                    echo "$input has vowels after the fourth character";
            }else{
                    echo "$input has no vowels after the fourth character";
            }
    
        }
    }
    
    checkstring($string);
    ?>
    

    【讨论】:

    • 这是什么?它的返回 "$input has no vowels" ?
    • @voodoo417 它在我的服务器上正常显示。
    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2019-05-02
    • 2013-10-10
    相关资源
    最近更新 更多