match innermost using regex in a scala string

时间:2016-12-09 12:56:09

标签: regex scala

I have sample data like below

header det1 det2 det3 header det1 det2 det3  innerdescription1 innerdescription2

I want to extract only innermost match using regex

i.e val detail = "header det1 det2 det3 header det1 det2 det3 innerdescription1 innerdescription2"

Here is my code :

val ptrn="""header .*? (innerdescription2)""".r
val head = ptrn.findAllIn(detail) 

But its giving the entire data again. i.e from first header to innerdescription2. I am not able to find out how to get inner match here. . Any ideas?

2 个答案:

答案 0 :(得分:0)

You can use lookups and I believe its supported in scala.

 (?<=header).*?(?=innerdescription2)

答案 1 :(得分:0)

\bheader\b(?:(?!header).)*\binnerdescription2

试试这个。看看演示。

https://regex101.com/r/uo9zpg/1