0

How to find all the file names that contain hello, English and end with .apk.

I currently have a regular expression that I use to find file names that contain hello and end with .apk.

preg_match('/.*hello.*\.apk$/i', $filename);

3 Answers 3

3

You could do that with regular expressions but you'll eventually end up with ugly/complex/illegible expression. Instead use conjunction of simplier conditions joined togheter with logic operators:

if (strpos($filename, 'hello') !== 0 && strpos($filename, 'English') !== 0 && substr($filename, -4) === '.apk') {
    // filename matches
}

PHP's lack of standard string functions like contains() or starts|endsWith() makes above code a little bit ugly, but there's nothing that stopes you from creating a helper, utility class:

class StringHelper {
    private $subject;

    public function __construct($subject) {
        $this->subject = $subject;
    }

    public function contains($substring) {
        return strpos($this->subject, $substring) !== false;
    }

    public function endsWith($string) {
        return substr($this->subject, strlen($string) * -1) === $string;
    }
}

Then your if becomes even simpler:

$filename = new StringHelper($filename);

if ($filename->contains('hello') && $filename->contains('English') && $filename->endsWith('.apk')) {
    // matches
}

Hint: Instead of creating StringHelper object you could use a "classical" StringUtilis class with a bunch of static methods that accepts $string as their first argument.

Hint: StringHelper::contains() could accept an array of strings and check whether your string contains them all or at least one of them (depending on some sort of a flag given in second argument).

Sign up to request clarification or add additional context in comments.

1 Comment

This gives an error for the use of $this when not in object context??
1

This regex should do the trick if you need 'hello' OR 'English' in the file name :

/.*(hello|English).*\.apk$/

7 Comments

but it will check only for hello and English
@Glavic - I disagree, it sounds like he needs OR.
@nickb: he wrote, i cite "How to find all the file names that contain hello, English and end with .apk.". To me, this is AND.
@Glavic, yes, and he also wrote "regular expression to match two different strings". If it were AND, he's only matching one string. See how magnetik is right when he says that the OP's requirements are not clear?
@nickb, then there is a conflict, question title and body are crossing. We will see what OP really wants.
|
1

If "English" must come after "hello":

if(preg_match('/.*hello.*English.*\.apk$/i', $filename));

If they can be in any order:

if(preg_match('/(hello.*English|English.*hello).*\.apk$/i', $filename));

3 Comments

Is there an version that states - must match hello and English (in any order) and ends with .apk?
'/(hello.*English|English.*hello).*\.apk$/i' should work, even if there's probably a more elegant way..
@ithcy, fix you answer for the future viewers.

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.