简体   繁体   English

Scala-正则表达式,用于不确定的重复模式

[英]Scala - Regex for uncertain repetitive pattern

In Scala, I've a string inputs like 在Scala中,我有一个字符串输入,例如

1. [[Country].[US]-[Country].[Canada]]
2. [[Country].[US]-[Country].[Canada]-[Country].[Mexico]]
3. [[Country].[US].[New York]-[Country].[US].[California]]

I need to extract following for above strings 我需要提取以下字符串

1. [Country].[US], [Country].[Canada]
2. [Country].[US], [Country].[Canada], [Country].[Mexico]
3. [Country].[US].[New York], [Country].[US].[California]

They are random, separated by - (hyphen). 它们是随机的,以-(连字符)分隔。

I need a regex that can match groups. 我需要一个可以匹配组的正则表达式。 I can make it work for static pattern, but don't know how to accommodate dynamic ones. 我可以使其适用于静态模式,但不知道如何适应动态模式。

I can't split it with hyphen, as there can be another input containing negative integers, like 我不能用连字符分割它,因为可能会有另一个包含负整数的输入,例如

 [Rank].[-1]-[Rank].[1]-[Rank].[10] 

You can simply drop the first and last character and split by "-" : 您只需删除第一个和最后一个字符并用"-"分割即可:

def extractTokens(text: String) = text.drop(1).dropRight(1).split("-")

> extractTokens("[[Country].[US]-[Country].[Canada]]")
> Array([Country].[US], [Country].[Canada])

> extractTokens("[[Country].[US]-[Country].[Canada]-[Country].[Mexico]]")
> Array([Country].[US], [Country].[Canada], [Country].[Mexico])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM