0

I am out of luck getting a regex build together which machtes everything but a specific character not preceded by a specific string.

For example this String:

Mr. Jones likes fish.

Should match like this:

Mr. Jones likes fish

And not for example:

Mr

I think this should be really easy but it just won't work. I already have it working for getting a correct match on the dot:

(?<!Mr)\.

The complementary match won't work though. I tried this:

(?<!Mr)[^.]*

Because i thought the cursor would be on the dot at that point and see that it was proceded by the "Mr" and it won't match. But it does.

And something like this:

(^((?<!Mr).))*

But the lookbehind in that regex is not recognized as one anymore and it just tries to match each character.

I already saw this answer but i couldn't find anythin helping me there.

amalloy
  • 75,856
  • 7
  • 133
  • 187
systderr
  • 55
  • 6

1 Answers1

3

As it looked to me, you want to match any character besides . with the exception that the period is preceded by Mr so you could use an alternation (or an optional group) and a lookbehind.

(?:[^.]+|(?<=Mr)\.)+

As a Java String: "(?:[^.]+|(?<=Mr)\\.)+"

Here is a demo at regex101 (non Java)

bobble bubble
  • 11,968
  • 2
  • 22
  • 34