TransWikia.com

how to create a regex that doesn't contain a text but starts with a specified text?

Stack Overflow Asked by lolozen on November 27, 2021

Hi I’d like to create a regex that check if my string starts with a / but no matter where shouldn’t contains /! so:

/xxxx = true

/xxxx/yyyy = true

/!xxxx = false

/xxxx/!yyyy = false

xxxx = false

I tried this but it doesn’t seems to work:

^/(?!(/!)(.*|/!.*))*$

One Answer

As /!xxxx should also be false, the lookahead should be at the start of the string or else you will miss checking the leading /.

To find /! anywhere in the string, you could prepend .*

^(?!.*/!)/.*

Regex demo

Answered by The fourth bird on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP