Skip to content
Advertisement

hava regex starts with and ends with and skipping a specific charachter

I would like to find a string that starts and ends with a specific special character but skipping when another specific charachter.

I want to find any string that starts with “hello”and ends with a dot “.” but not like “here end.” this must be skipped when “here end” is preceding the dot “.”.

Sample :

String para = "XXXX@gxxl.com. this will share the costs and hello you  the initial here end. must be carrefully done.Keep going."

I tried the following regex but its not working:

hello.+?((?=([.]))

I got this :

"hello you  the initial here end."

but this is required :

"hello you  the initial here end. must be carrefully done."

Advertisement

Answer

You can yse

hello.*?(?<!here end).

See the regex demo. Details:

  • hello – a fixed string
  • .*? – any zero or more chars other than line break chars as few as possible
  • (?<!here end). – a . that is not immediately preceded with here end.

If you need to exclude the . from match, put it into a lookaround, hello.*?(?<!here end)(?=.).

Advertisement