Regex 如何使用正则表达式仅检索单词
使用正则表达式,我如何只检索单词,而忽略任何其他符号,如逗号、数字等Regex 如何使用正则表达式仅检索单词,regex,scala,Regex,Scala,使用正则表达式,我如何只检索单词,而忽略任何其他符号,如逗号、数字等 val words = text.split("\b([-A-Za-z])+\b") 例如: This is a nice day, my name is... 我想得到: This, is, a, nice, day, my, name, is 忽略和……在非字母上拆分字符串时: val words = text.split("[^-A-Za-z]+") 在非字母上拆分字符串: val words = text.sp
val words = text.split("\b([-A-Za-z])+\b")
例如:
This is a nice day, my name is...
我想得到:
This, is, a, nice, day, my, name, is
忽略和……在非字母上拆分字符串时:
val words = text.split("[^-A-Za-z]+")
在非字母上拆分字符串:
val words = text.split("[^-A-Za-z]+")
要提取所有单词(包括连字符单词),可以使用
"""\b[a-zA-Z]+(?:-[a-zA-Z]+)*\b""".r.findAllIn(s)
使用{p}类中的{Z-p}字符代替}
val s = "This is a nice day, my name is..."
val res = """\b\p{L}+(?:-\p{L}+)*\b""".r.findAllIn(s)
println(res.toList)
// => List(This, is, a, nice, day, my, name, is)
请参阅。要提取所有单词,包括连字符单词,您可以使用
"""\b[a-zA-Z]+(?:-[a-zA-Z]+)*\b""".r.findAllIn(s)
要支持所有Unicode字母,请使用\p{L}而不是[a-zA-Z]字符类:
val s = "This is a nice day, my name is..."
val res = """\b\p{L}+(?:-\p{L}+)*\b""".r.findAllIn(s)
println(res.toList)
// => List(This, is, a, nice, day, my, name, is)
看
答复:
答复:
你能解释一下符号+的意思吗?@ScalaBoy:它表示前面的字符出现一次或多次。有关更多信息,请参阅和。请解释符号+的含义好吗?@ScalaBoy:它表示前面的字符出现一次或多次。有关更多信息,请参阅和。在javascript中,我将使用str.split/[,\?\。\d]/.filterBoolean;我使用过滤器是因为数组中可能会有空字符串。@enxaneta我使用Scala作为post的标记。在javascript中,我将使用str.split/[,\?\。\d]/.filterBoolean;我使用过滤器是因为数组中可能会有空字符串。@enxaneta我使用Scala作为post的标记。只需要一对[]:[a-zA-Z]+。只需要一对[]:[a-zA-Z]+。r