Reputation: 10143
I've below scala function.
def getMatchesFor(input: String, matchId: String): List[String] = {
val regex = s"""${matchId}_\w*""".r
(regex findAllIn input).toList
}
What is the expectation of the function is
val input = "abc_1_1_1_0abc_1_0_1"
val result = getMatchesFor(input, "abc")
result
should be List("abc_1_1_1_0", "abc_1_0_1")
I've tried so far val r = s"${matchId}_\\w*".r
but it is not working as expected.
How can I fix this issue?
Upvotes: 1
Views: 55
Reputation: 163207
Another option using split and a positive lookahead.
See the positions for the split.
def getMatchesFor(input: String, matchId: String): List[String] = {
val regex = s"""(?=${matchId}_)""".r
regex.split(input).toList
}
Output
List(abc_1_1_1_0, abc_1_0_1)
Upvotes: 1
Reputation: 925
Here's a regex that matches your needs:
val regex = s"${matchId}_\\w*?((?=${matchId})|\\W|$$)".r
\w*
must be none greedy, otherwise it would just match everything. It should only match until followed by:
matchId
using a lookahead (?=${matchId})
\W
$
An alternative approach might be to simply split the string using matchId
and extract the remaining match _\w*
from the parts (ignoring the first split).
Upvotes: 2