Oto Shavadze
Oto Shavadze

Reputation: 42813

REGEXP pattern for matching 2 different symbols

I need that if string contains symbols . AND @ that print yes, queue symbols does not matters, important that in string will be this symbols at least one times, I need make this using regexp.

I write:

if (preg_match("#(\@.*\.)|(\..*\@)#",$str)) {
    echo "yes";
}

But I doubt that can write more easy pattern for this

Upvotes: 0

Views: 83

Answers (3)

René Höhle
René Höhle

Reputation: 27305

Perhaps its a bit easier without a regex.

if(strpos($str, ".") !== false || strpos($str, "@") !== false) {
    echo "yes";
}

Then you don't need the regex is perhaps a bit faster. Then you only search if a character is in a string.

Upvotes: 0

John Dvorak
John Dvorak

Reputation: 27307

You can use lookahead to separate the two conditions:

^(?=.*\.)(?=.*@)

The start-of-string anchor is not needed, but it helps performance.

Upvotes: 2

burning_LEGION
burning_LEGION

Reputation: 13450

use this regex (?=.*\.)(?=.*@).+

(?=.*\.) dot exists
(?=.*@) @ exists
.+ any string

Upvotes: 2

Related Questions