Mild Fuzz
Mild Fuzz

Reputation: 30691

why does this regex fail in PHP?

I got the expression directly from RegExr, but PHP has a problem with the =

"/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/"

The expression is for matching email addresses.

Upvotes: 1

Views: 66

Answers (3)

codaddict
codaddict

Reputation: 455092

You are using / as delimiter. There are two / in the regex which are not escaped. Escape them as \/:

"/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/"
                 ^^                                ^^

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

You used / as the delimiter marking the start and end of the pattern, but then also used that character within the pattern. You must either use a different delimiter, or escape instances of it within the pattern. If you meant to escape the equals signs, then you used the wrong slash.

Upvotes: 3

Joey
Joey

Reputation: 354566

Escape the slash preceding the = (and the other slash in that expression). You use / as a delimiter, therefore if it occurs inside the pattern it has to be escaped.

"/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/"

should work, then.

Upvotes: 2

Related Questions