Nikolay
Nikolay

Reputation: 165

How do I recognize strings that do not end with a slash character ('/') using a regex?

How can i match a string that does not finish with / . I know I can do that /\/$/ and it will match if string does finish with /, but how can I test to see if it doesn't?

Upvotes: 4

Views: 6191

Answers (3)

ElGaucho
ElGaucho

Reputation: 408

[^\/]$

^ will negate any character class expression.

Upvotes: 0

Donald Miner
Donald Miner

Reputation: 39893

You can say "not character" by doing [^...]. In this case, you can say "not backslash by doing": /[^\/]$/

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838036

You can use a negative character class:

/[^\/]$/

This however requires that the string contains at least one character. If you also want to allow the empty string you can use an alternation:

/[^\/]$|^$/

A different approach is to use a negative lookbehind but note that many popular regular expression engines do not support lookbehinds:

/(?<!\/)$/

Upvotes: 13

Related Questions