4

How can I set a regex pattern which matches all words but the strings which starts with

  1. /word
  2. /word/
  3. /word/ following by anything else.

I think the pattern starts with \A but I don'0t know how to tell that should not follow a word

Thanks

Juanillo
  • 875
  • 2
  • 11
  • 17

4 Answers4

5

Use this kind of negate regex and replace word by your word.

^((?!word).)*$
niksvp
  • 5,545
  • 2
  • 24
  • 41
2

You can use the regex:

^(?!\\/word).*$

See it

codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Take a look at the lookaround feature offered by regular expressions. Also, a similar thread. Also, posting your question in terms of a concrete sample problem might help you get a few working sample snippets.

Community
  • 1
  • 1
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
1

Perhaps using a string comparison will be clearer and faster.

if (text.startsWith("word")) {
   // text is OK
} else {
   // not OK
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130