-3

what I want is a regex that gets words that contains for example character a and does not contain characters b and c

it seemed to me that the following does half of the job.

^[^bc]+$

but I want words. and containing a is not considered here.

taiga
  • 15
  • 4
  • 1
    If you mean words in longer texts, `\b[ad-z]+\b`. – Wiktor Stribiżew Sep 13 '21 at 10:20
  • @WiktorStribiżew that does correctly gives the words without `b` and `c`. but `b` and `c` were just examples, I need words that contain some characters and don't contain some other characters – taiga Sep 13 '21 at 10:50
  • sorry if I came out as condescending. what I need is a general solution. for example with `^[^bc]+$` you can replace `b` and `c` with any character you want. but it is not possible with your solution. – taiga Sep 13 '21 at 11:01
  • You need one of the solutions in [Exclude characters from a character class](https://stackoverflow.com/questions/17327765/exclude-characters-from-a-character-class) – Wiktor Stribiżew Sep 13 '21 at 11:09

1 Answers1

1

Use a negative look ahead (?!...) to make sure that the word that we will capture has no b nor c. Then, match if the word has any a.

\b(?!\w*[bc]\w*)\w*a\w*\b

enter image description here

Where:

  • \b - Match the word boundary marking the start of the word
  • (?!\w*[bc]\w*) - Negative look ahead. Match the next characters (which is the word) only if it doesn't contain "b" nor "c".
  • \w*a\w* - Match the word if it contains any "a"
  • \b - Match the word boundary marking the end of the word