Matching strings comprised only of consonants

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
kenja
Forum Newbie
Posts: 4
Joined: Mon Apr 21, 2008 11:02 pm

Matching strings comprised only of consonants

Post 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!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching strings comprised only of consonants

Post 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!
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Matching strings comprised only of consonants

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Matching strings comprised only of consonants

Post 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.
User avatar
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

Post 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...*/}
kenja
Forum Newbie
Posts: 4
Joined: Mon Apr 21, 2008 11:02 pm

Re: Matching strings comprised only of consonants

Post by kenja »

Thanks. Works like a charm.
Post Reply