Nick
Nick

Reputation: 3965

PHP - searching a string using multiple wildcards

I'm trying to figure if a string starts with the characters US, DS, UCS or DCS.

As tempting as it was to just do an unnecessarily long if statement, I figured there's a better way, I've been trying to execute this using regular expressions but have had no joy:

$string = 'USQWERTY';
if(preg_match_all('|(US|DS|UCS|DCS)|', $string) // do something

It always returns false, I'm wondering if it's because I haven't used a wildcard? I've tried using *s after each word but haven't had any joy with that either unfortunately.

Would really appreciate any advice on this, thanks!

Upvotes: 1

Views: 394

Answers (5)

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

I am not a regex expert, mainly because it just confuses the beejeebus out of me. I wrote this function really fast that can check for you.

  function searchMultiple($n, $h) {
    for ($i = 0; $i < count($n); $i++) {
        if (stripos($h, $n[$i]) !== false) return TRUE;
    }
  }

  $needles = array("US","DS","UCS","DCS");
  $haystack = "We are looking for one of our needles in here, like maybe 'US'";
  searchMultiple($needles, $haystack);

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838376

You should use a different delimiter:

'/(US|DS|UCS|DCS)/'

I noticed also you said this:

I'm trying to figure if a string starts with the characters US, DS, UCS or DCS.

The regular expression you are using will match those characters anywhere in the string. You should also use a start of string anchor to only match at the start of the string:

'/^(US|DS|UCS|DCS)/'

You are also missing a closing parenthesis and it looks like you want to use preg_match instead of preg_match_all.

if (preg_match('/^(US|DS|UCS|DCS)/', $string))

Upvotes: 6

raina77ow
raina77ow

Reputation: 106385

First, you really don't need preg_match_all, if what you want is one match. preg_match will be enough.

Second, if you use pipe symbol as a global pattern delimiter, you'll have to escape it within the pattern itself. It's easier just to choose the usual forward slash symbol. )

Finally, if you need to match only when the string begins (with the series of letters), you'll have to alter the regex slightly:

if (preg_match('/^(?:US|DS|UCS|DCS)/', $string)) { ... }

Works for me in this example:

<?php

$string = 'USdsa';
if (preg_match('/^(?:US|DS|UCS|DCS)/', $string)) { 
  echo 'Matched!'; 
}

$string = 'DCSda';
if (preg_match('/^(?:US|DS|UCS|DCS)/', $string)) { 
  echo 'Matched again!'; 
}

$string = 'EWDS';
if (! preg_match('/^(?:US|DS|UCS|DCS)/', $string)) { 
  echo 'Not matched!'; 
}

?>

Upvotes: 5

Toote
Toote

Reputation: 3413

Your issue is that PCRE patterns need to be enclosed within delimiters and if you want to include your delimiters within the pattern, you need to escape it.

So if you change your regex to one of the following, it should work:

  • '/(US|DS|UCS|DCS)/'
  • '|(US\|DS\|UCS\|DCS)|'

Though I'd strongly suggest the first form as it is combersome to use a symbol with a lot of meaning within a regular expression as the PCRE delimiter.

Upvotes: 1

Matthew Riches
Matthew Riches

Reputation: 2286

Pipe means OR, so you need to remove this from the start and end and also make sure its case insensitive

/^(US|DS|UCS|DCS)/gi

Upvotes: 1

Related Questions