0

I have a question about scala regex

The thing I need to do is given a string, I need to find a sub-string up to a specific word given. For example, my regular expression looks like following

val x= "(?s)^(.*)(?=(foo|bar)".r

Then given a string, I need to find the longest sub-string until before foo or bar. This works perfectly but I would like to get the whole string if the string does not contain foo or bar at all.

Right now if I do

x.findAllIn("hello nice to meet you").toArray

it gives me an empty string but I would like to get "hello nice to meet you" when I do that.

Does anyone have an idea how to implement that?

mrzasa
  • 22,895
  • 11
  • 56
  • 94
김성보
  • 1
  • 2

1 Answers1

0

You can add an end-of-string assertion to the alternative:

(?s)^(.*?)(?=(foo|bar|$))

Demo

mrzasa
  • 22,895
  • 11
  • 56
  • 94