user3110754
user3110754

Reputation: 3

Regular expression to give no match on specific character

I use test_([^/]+) but this only ignore the / character.

I want to give no match if / found anywhere after test_.

Example, what I need:

test_apple matches apple

test_apple/ gives no match.

test_ap/le gives no match.

How should I do this? Thanks!

Upvotes: 0

Views: 47

Answers (2)

Vasili Syrakis
Vasili Syrakis

Reputation: 9601

You can use a negative lookahead to accomplish this:

(?<=test_)(apple)(?![/])

A Negative Lookahead basically means: "If you find this after this position, don't match."

Upvotes: 0

strikernl
strikernl

Reputation: 55

How about: test_([^/]+)$

$ means look until the end of the string

Upvotes: 3

Related Questions