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:
a-z
p
, the second letter has to be p
or after it in the alphabet)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.
[A-Za-z] will match all the alphabets (both lowercase and uppercase). ^ and $ will make sure that nothing but these alphabets will be matched.
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 .
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With