1

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!

5 Answers 5

6

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, it always seems to return false though, even if I change it to match the entire thing: preg_match_all('/^(USQWERTY|DS|UCS|DCS)/', $string)
I am an idiot, sorry - I only just realised preg_match_all matches against all of those, rather than checking against all of them individually!
@Nick: I don't think you are an idiot. I think you just haven't read the PHP manual. Probably because you didn't know it existed. I suggest you read it, or if you don't have time to read it, at least use it as a reference every time you get stuck.
5

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!'; 
}

?>

1 Comment

Many thanks, lots of great answers here but I'll mark this as the correct answer because you noticed that I stupidly used the wrong preg_match function! Thank you :)
1

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

1 Comment

g is global and i is case insensitive.
1

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.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.