I'm writing an expression that matches strings that only include consonants and numbers. I'm passing in strings without any spaces and the following expression matches all the right strings. However, it also matches strings that start with consonants.
([b-df-hj-np-tv-z0-9]+)
Example returns:
hello -> returns: h
kjflq5 - returns: kjflq5
How do I modify that expression so it only matches strings that are ALL consonants.
I tried putting in a ^ in the beginning and a $ at the end, but that didn't do the trick.
Thanks!
Matching strings comprised only of consonants
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Matching strings comprised only of consonants
preg_match(...) returns true if it finds just one match in your string. So, you will need to anchor the beginning and and of your input string in your regex.kenja wrote:I'm writing an expression that matches strings that only include consonants and numbers. I'm passing in strings without any spaces and the following expression matches all the right strings. However, it also matches strings that start with consonants.
([b-df-hj-np-tv-z0-9]+)
Example returns:
hello -> returns: h
kjflq5 - returns: kjflq5
Yes, that should do the trick. I am guessing you misplaced those anchors, because this works like a charm:kenja wrote: How do I modify that expression so it only matches strings that are ALL consonants.
I tried putting in a ^ in the beginning and a $ at the end, but that didn't do the trick.
Thanks!
Code: Select all
#!/usr/bin/php
<?php
$tests = array("hello","kjflq5","kjiflq5","hll");
foreach ($tests as $t) {
if(preg_match('/^[b-df-hj-np-tv-z0-9]+$/', $t)) {
echo "'$t' is accepted!\n";
} else {
echo "'$t' is rejected...\n";
}
}
?>Code: Select all
'hello' is rejected...
'kjflq5' is accepted!
'kjiflq5' is rejected...
'hll' is accepted!Re: Matching strings comprised only of consonants
preg_match() returns an integer: the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match() returns FALSE if an error occurred.prometheuzz wrote:preg_match(...) returns true if it finds just one match in your string.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Matching strings comprised only of consonants
Ah, I see, thanks for the correction. I'm used to using Java's regex engine: hence the "true" or "false" assumption.GeertDD wrote:preg_match() returns an integer: the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match() returns FALSE if an error occurred.prometheuzz wrote:preg_match(...) returns true if it finds just one match in your string.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Re: Matching strings comprised only of consonants
Don't forget that in PHP 0 is "falsy", so you'll want to compare with the === operator:
Code: Select all
if(preg_match('/^[b-df-hj-np-tv-z0-9]+$/', $t)===1){/*...success...*/}Re: Matching strings comprised only of consonants
Thanks. Works like a charm.