如何在Scala中拆分String但保持部件与正则表达式匹配?

Noe*_*Yap 6 regex scala

我的问题与Split字符串相同,包括正则表达式匹配,但对于Scala.不幸的是,JavaScript解决方案在Scala中不起作用.

我正在解析一些文字.假设我有一些字符串:

"hello wold <1> this is some random text <3> foo <12>"
Run Code Online (Sandbox Code Playgroud)

我想得到以下Seq : "hello world" :: "<1>" :: "this is some random text" :: "<3>" :: "foo" :: "<12>".

请注意,每当遇到<"number">序列时,我都会分割字符串.

Ako*_*chy 5

val s = "hello wold <1> this is some random text <3> foo <12>"
s: java.lang.String = hello wold <1> this is some random text <3> foo <12>

s.split("""((?=<\d{1,3}>)|(?<=<\d{1,3}>))""")
res0: Array[java.lang.String] = Array(hello wold , <1>,  this is some random text , <3>,  foo , <12>)
Run Code Online (Sandbox Code Playgroud)

你真的试过你的编辑吗?有\d+不起作用.看到这个问题.

s.split("""((?=<\d+>)|(?<=<\d+>))""")
java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 19
Run Code Online (Sandbox Code Playgroud)