Patan
Patan

Reputation: 17873

Regex to match slash does not work

I want a regex to match these:

/web/search/abc
/web/search/employee/999999999
/web/search/employee/78524152

But not this:

/web/search/employee/123456789

I wrote the following regex for Java, but it does not seem to be working.

/web/search/(?!/(employee/123456789)).*

Can someone tell me the correct regex to do this?

Upvotes: 0

Views: 105

Answers (4)

dnl-blkv
dnl-blkv

Reputation: 2128

Try the following:

/web/search/(?!(employee/123456789$)).*

The slash was doubled in the negating look-ahead group.

Upvotes: 2

Rakesh KR
Rakesh KR

Reputation: 6527

What you tried is : /web/search/(?!/(employee/123456789)) can be represented as

enter image description here

You need to change it as /web/search/(?!employee/123456789) can be represented as

enter image description here

Upvotes: 3

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

I would say

str.contains("^/web/search/(?!employee/123456789)")

satisfies your requirements.

Online demonstration here: http://regex101.com/r/dF1eU9

Upvotes: 2

fge
fge

Reputation: 121710

This is because you double the / in the lookahead. Try with:

/web/search/(?!employee/123456789$).*

Upvotes: 3

Related Questions