1

I would like to match all chars except those specific one at the end of a word if it exists or not, for instance:

  • fooBarsBar
  • fooBars

I would like it match for both example "fooBars" and "fooBars" without "Bar" at then end even if it does not exist

I tried:

(.*)(?=Bar)|(.*)

And also this:

[^Bar]*

For the first regex it captures all by group and the second one it captures all chars except "Bar" but not the one at the end...

Could you please help me, thanks

John_Trump
  • 53
  • 5
  • Words or strings? Try also [`^.*?(?=(?:Bar)?$)`](https://regex101.com/r/TziVlE/1). – Wiktor Stribiżew Aug 11 '18 at 22:37
  • 1
    Possible duplicate of [How to negate specific word in regex?](https://stackoverflow.com/questions/1240275/how-to-negate-specific-word-in-regex) – Nondv Aug 11 '18 at 22:39

1 Answers1

2

There are different ways but your first regex pattern looks good already.

Add anchors ^ for start and $ end inside the lookahead and use + quantifier for one or more.

^.+(?=Bar$)|^.+

Here is a demo at regex101

Further be aware that [^Bar] represents a negated character class matching a character, that is not listed in the set. It won't match substrings that are not Bar as it looks you thought.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46