Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if string has no vowel after 4 consecutive chars [closed]

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

Consider

$String = 'therhythm';

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.

  • Should also not contain 3 consecutive vowels,
  • Should not contain numbers,
  • Non Alphabetic chars
like image 822
user3109875 Avatar asked Dec 31 '13 08:12

user3109875


3 Answers

Based on your latest edit: You can use following regex for this:

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

Online Demo: http://regex101.com/r/vM0aN9

Breaking it down:

  • [^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
like image 87
anubhava Avatar answered Nov 14 '22 23:11

anubhava


Try this

<?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);
?>
like image 42
Adlin Ling Avatar answered Nov 14 '22 22:11

Adlin Ling


^(?![aeiou]{3,})(?:\D(?![^aeiou]{4,}[aeiou]*)(?![aeiou]{4,})){3,}$

This regex has all your conditions:

<?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";
    }

Output:

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
like image 22
revo Avatar answered Nov 15 '22 00:11

revo