3

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
  • 835
  • 2
  • 11
  • 16

4 Answers4

4

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

^((?!word).)*$
niksvp
  • 5,475
  • 2
  • 23
  • 40
2

You can use the regex:

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

See it

codaddict
  • 429,241
  • 80
  • 483
  • 523
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,282
  • 4
  • 58
  • 71
1

Perhaps using a string comparison will be clearer and faster.

if (text.startsWith("word")) {
   // text is OK
} else {
   // not OK
}
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106