homelessDevOps
homelessDevOps

Reputation: 20726

regExp problem - string is matched but it should not match

iam trying to check if an user has permission to manage an group:

Expression (ou=|||) is the string I'm looking for

/^OU=|||$|,OU=|||$/i

On a string like "ou=whatever", it returns true (-:

I am sure it's a problem with the pipes, but I have no idea how to solve this.

I am using PHP 5.x with preg_match.

Upvotes: 1

Views: 58

Answers (3)

Álvaro González
Álvaro González

Reputation: 146558

$has_permission = in_array('OU=|||', explode(',', $permission_string));

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

Pipes are metacharacters in a regular expression (meaning "or"). You need to escape them:

/^OU=\|{3}$|,OU=\|{3}$/i

Are you sure that you're using the start- and end-of-string anchors correctly? Right now, this regex will only match the strings

OU=|||

and

<any number of characters>,OU=|||

Upvotes: 3

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174397

You need to escape the pipes and include some parenthesis for better readability:

/(^OU=\|\|\|$)|(,OU=\|\|\|$)/i

Upvotes: 1

Related Questions