Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for finding letters before or after a certain letter

I have the following array in PHP:

$words = array("apple", "banana", "lemon");

I want to search for a word, where it matches the following profile:

  • The first letter can be anything a-z
  • The second letter must be the same letter as, or after the first letter (i.e. if the first letter was p, the second letter has to be p or after it in the alphabet)
  • The third letter must be the same letter as, or after the second letter
  • The fourth letter must be before the third letter
  • The fifth letter must be before the fourth letter

Is there any way to create a regular expression that can match the above conditions? This would be best, as I am looking to also create an implementation in MySQL, so regular expressions would be more transferable to the new situation.

like image 856
Lucas Avatar asked Mar 28 '16 10:03

Lucas


People also ask

How do I find regular expressions with letters?

[A-Za-z] will match all the alphabets (both lowercase and uppercase). ^ and $ will make sure that nothing but these alphabets will be matched.

How do you match anything up until this sequence of characters in regular expression?

Take this regular expression: /^[^abc]/ . This will match any single character at the beginning of a string, except a, b, or *c. If you add a * after it – /^[^abc]*/ – the regular expression will continue to add each subsequent character to the result, until it meets either an a , or b , or c .

What is the regular expression matching one or more specific characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .

What does this regular expression mean [\ s ]+$?

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r] The plus sign + is a greedy quantifier, which means one or more times. For example, expression X+ matches one or more X characters.


1 Answers

I came up with a way to do it without RegEx, however your conditions will still be matched:

function my_func($str) {
    $letters = 'abcdefghijklmnopqrstuvwxyz';

    $match = true; // Will be set to false if does not match conditions

    $l1pos = strrpos($letters, $str[0]);
    $l2pos = strrpos($letters, $str[1]);
    $l3pos = strrpos($letters, $str[2]);
    $l4pos = strrpos($letters, $str[3]);
    $l5pos = strrpos($letters, $str[4]);

    // If letter 2 comes before letter 1
    if ($l2pos < $l1pos) { $match = false;}

    // If letter 3 comes before letter 2
    if ($l3pos < $l2pos) { $match = false; }

    // If letter 4 comes after letter 3
    if ($l4pos >= $l3pos) { $match = false; }

    // If letter 5 comes after letter 4
    if ($l5pos >= $l4pos) { $match = false; }

    return $match;
}

You can use it like so:

$string = 'apple';

if (my_func($string)) {
    print 'Matched!';
}
else {
    print 'Not Matched. :(';
}

If you want to make the function really small, you could use the following:

function my_func($str) {
    $letters = 'abcdefghijklmnopqrstuvwxyz';
    $match = true;
    function m($i) {
        return strrpos($letters, $str[$1]);
    }
    if ((m(1) < m(0)) || (m(2) < m(1)) || (m(3) >= m(2)) || (m(4) >= m(3))) {
        $match = false;
    }
    return $match;
}

I did also experiment with a RegEx, and got the following:

^([a-z])    # First Letter
([\1-z])    # Second Letter
([\2-z])    # Third Letter
([a-\3])    # Fourth Letter
([a-\4])    # Fifth Letter

However, you cannot use a-z while dynamically setting a or z to one of the previous captured groups. You could use PHP concatenation to create a RegEx, however this would require at least 4 lines of code, for each letter but the first.

like image 156
Kaspar Lee Avatar answered Oct 28 '22 14:10

Kaspar Lee