Page 1 of 1

Matching strings comprised only of consonants

Posted: Thu Apr 24, 2008 12:34 pm
by kenja
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!

Re: Matching strings comprised only of consonants

Posted: Fri Apr 25, 2008 3:22 am
by prometheuzz
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
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: 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!
Yes, that should do the trick. I am guessing you misplaced those anchors, because this works like a charm:

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";
    }
  }
?>
And produces the following output:

Code: Select all

'hello' is rejected...
'kjflq5' is accepted!
'kjiflq5' is rejected...
'hll' is accepted!
HTH!

Re: Matching strings comprised only of consonants

Posted: Tue Apr 29, 2008 3:36 am
by GeertDD
prometheuzz wrote:preg_match(...) returns true if it finds just one match in your string.
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.

Re: Matching strings comprised only of consonants

Posted: Tue Apr 29, 2008 5:13 am
by prometheuzz
GeertDD wrote:
prometheuzz wrote:preg_match(...) returns true if it finds just one match in your string.
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.
Ah, I see, thanks for the correction. I'm used to using Java's regex engine: hence the "true" or "false" assumption.

Re: Matching strings comprised only of consonants

Posted: Tue Apr 29, 2008 5:27 am
by Kieran Huggins
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

Posted: Tue Apr 29, 2008 8:35 am
by kenja
Thanks. Works like a charm.