If you want to do this with maximum efficiency, you may have to write it yourself (or find a good substring search algorithm somewhere). If you just want it to work at all, then in Scala:
scala> "Finding needle in haystack" contains "needle" res0: Boolean = true scala> "Finding needle in haystack" indexOf "needle" res1: Int = 8
This is not a regular expression search. You also do not use a regular expression (edit: because this code asks for an exact match with the entire string, and not for finding the appropriate substring), but this is another problem. If you want to count the number of matches, you can do something like
scala> "needle".r.findAllIn("Finding needle in haystack").length res2: Int = 1
Rex Kerr Apr 12 2018-12-12T00: 00Z
source share