I want to extract a list of ID of a string pattern in the following: {(2),(4),(5),(100)}
我想在以下内容中提取字符串模式的ID列表:{(2),(4),(5),(100)}
Note: no leading or trailing spaces.
注意:没有前导或尾随空格。
The List can have up to 1000 IDs.
列表最多可包含1000个ID。
I want to use rich string pattern matching to do this. But I tried for 20 minutes with frustration.
我想使用丰富的字符串模式匹配来做到这一点。但我沮丧地试了20分钟。
Could anyone help me to come up with the correct pattern? Much appreciated!
任何人都可以帮我提出正确的模式吗?非常感激!
3 个解决方案
#1
3
Here's brute force string manipulation.
这是蛮力字符串操作。
scala> "{(2),(4),(5),(100)}".replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("\\{","").replaceAll("\\}","").split(",")
res0: Array[java.lang.String] = Array(2, 4, 5, 100)
Here's a regex as @pst noted in the comments. If you don't want the parentheses change the regular expression to """\d+""".r
.
这是@pst在评论中提到的正则表达式。如果您不希望括号将正则表达式更改为“”“\ d +”“”。r。
val num = """\(\d+\)""".r
"{(2),(4),(5),(100)}" findAllIn res0
res33: scala.util.matching.Regex.MatchIterator = non-empty iterator
scala> res33.toList
res34: List[String] = List((2), (4), (5), (100))
#2
2
"{(2),(4),(5),(100)}".split ("[^0-9]").filter(_.length > 0).map (_.toInt)
Split, where char is not part of a number, and only convert non-empty results.
拆分,其中char不是数字的一部分,只转换非空结果。
Might be modified to include dots or minus signs.
可能会被修改为包括点或减号。
#3
0
Use Extractor object:
使用Extractor对象:
object MyList {
def apply(l: List[String]): String =
if (l != Nil) "{(" + l.mkString("),(") + ")}"
else "{}"
def unapply(str: String): Some[List[String]] =
Some(
if (str.indexOf("(") > 0)
str.substring(str.indexOf("(") + 1, str.lastIndexOf(")")) split
"\\p{Space}*\\)\\p{Space}*,\\p{Space}*\\(\\p{Space}*" toList
else Nil
)
}
// test
"{(1),(2)}" match { case MyList(l) => l }
// res23: List[String] = List(1, 2)
#1
3
Here's brute force string manipulation.
这是蛮力字符串操作。
scala> "{(2),(4),(5),(100)}".replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("\\{","").replaceAll("\\}","").split(",")
res0: Array[java.lang.String] = Array(2, 4, 5, 100)
Here's a regex as @pst noted in the comments. If you don't want the parentheses change the regular expression to """\d+""".r
.
这是@pst在评论中提到的正则表达式。如果您不希望括号将正则表达式更改为“”“\ d +”“”。r。
val num = """\(\d+\)""".r
"{(2),(4),(5),(100)}" findAllIn res0
res33: scala.util.matching.Regex.MatchIterator = non-empty iterator
scala> res33.toList
res34: List[String] = List((2), (4), (5), (100))
#2
2
"{(2),(4),(5),(100)}".split ("[^0-9]").filter(_.length > 0).map (_.toInt)
Split, where char is not part of a number, and only convert non-empty results.
拆分,其中char不是数字的一部分,只转换非空结果。
Might be modified to include dots or minus signs.
可能会被修改为包括点或减号。
#3
0
Use Extractor object:
使用Extractor对象:
object MyList {
def apply(l: List[String]): String =
if (l != Nil) "{(" + l.mkString("),(") + ")}"
else "{}"
def unapply(str: String): Some[List[String]] =
Some(
if (str.indexOf("(") > 0)
str.substring(str.indexOf("(") + 1, str.lastIndexOf(")")) split
"\\p{Space}*\\)\\p{Space}*,\\p{Space}*\\(\\p{Space}*" toList
else Nil
)
}
// test
"{(1),(2)}" match { case MyList(l) => l }
// res23: List[String] = List(1, 2)