在 Scala Regex 中获取两个相同单词之间的字符串
Get string between two same words in Scala Regex
我正在尝试用 Scala 编写一个正则表达式解析器,它将抓取两个指定单词(在本例中为相同单词)之间的所有内容。这是我写的解析器:
def getBatchModules: Parser[String] = """(?s)(?=--batch.*?(?=--batch))""".r
输入如下:
val tempVal = "--batch_123123\n--batch_222222-\n--batch_asdkokasdj"
我希望我的解析器从中提取 --batch_123123。
当我 运行 我的代码时,我得到
string matching regex `\z' expected but `-' found
--batch_123123
^
有什么想法吗?
你必须修改你的正则表达式并使用另一个这样的:
(?s)--batch(.*?)--batch
然后像这样访问capturin组:
val string = "--batch_123123\n--batch_222222-\n--batch_asdkokasdj"
val pattern = """(?s)--batch(.*?)--batch""".r
pattern.findAllIn(string).matchData foreach {
m => println(m.group(1))
}
我正在尝试用 Scala 编写一个正则表达式解析器,它将抓取两个指定单词(在本例中为相同单词)之间的所有内容。这是我写的解析器:
def getBatchModules: Parser[String] = """(?s)(?=--batch.*?(?=--batch))""".r
输入如下:
val tempVal = "--batch_123123\n--batch_222222-\n--batch_asdkokasdj"
我希望我的解析器从中提取 --batch_123123。
当我 运行 我的代码时,我得到
string matching regex `\z' expected but `-' found
--batch_123123
^
有什么想法吗?
你必须修改你的正则表达式并使用另一个这样的:
(?s)--batch(.*?)--batch
然后像这样访问capturin组:
val string = "--batch_123123\n--batch_222222-\n--batch_asdkokasdj"
val pattern = """(?s)--batch(.*?)--batch""".r
pattern.findAllIn(string).matchData foreach {
m => println(m.group(1))
}