简体   繁体   English

通过 Scala 中的正则表达式模式匹配提取字符串的重复部分

[英]Extract the repetitive parts of a String by Regex pattern matching in Scala

I have this code for extracting the repetitive : separated sections of a regex, which does not give me the right output.我有这段代码用于提取正则表达式的重复:分隔部分,它没有给我正确的 output。

val pattern = """([a-zA-Z]+)(:([a-zA-Z]+))*""".r

for (p <- pattern findAllIn "it:is:very:great just:because:it is") p match {

     case pattern("it", pattern(is, pattern(very, great))) => println("it: "+ is + very+ great)

     case pattern(it, _,rest) => println( it+" : "+ rest)

     case pattern(it, is, very, great) => println(it +" : "+ is +" : "+ very +" : " + great)

     case _ => println("match failure")
   }

What am I doing wrong?我究竟做错了什么?

How can I write a case expression which allows me to extract each : separated part of the pattern regex?如何编写允许我提取每个: pattern正则表达式的分隔部分的 case 表达式?

What is the right syntax with which to solve this?解决这个问题的正确语法是什么?

How can the match against unknown number of arguments to be extracted from a regex be done?如何与从正则表达式中提取的未知数量的 arguments 进行匹配?

In this case print:在这种情况下打印:

it : is : very : great

just : because : it

is

You can't use repeated capturing group like that, it only saves the last captured value as the current group value.您不能像那样使用重复捕获组,它只会将最后捕获的值保存为当前组值。

You can still get the matches you need with a \b[a-zA-Z]+(?::[a-zA-Z]+)*\b regex and then split each match with : :您仍然可以使用\b[a-zA-Z]+(?::[a-zA-Z]+)*\b正则表达式获得所需的匹配,然后使用 : 拆分每个匹配:

val text = "it:is:very:great just:because:it is"
val regex = """\b[a-zA-Z]+(?::[a-zA-Z]+)*\b""".r
val results = regex.findAllIn(text).map(_ split ':').toList
results.foreach { x => println(x.mkString(", ")) }
// => it, is, very, great
//    just, because, it
//    is

See the Scala demo .请参阅Scala 演示 Regex details :正则表达式详细信息

  • \b - word boundary \b - 单词边界
  • [a-zA-Z]+ - one or more ASCII letters [a-zA-Z]+ - 一个或多个 ASCII 字母
  • (?::[a-zA-Z]+)* - zero or more repetitions of (?::[a-zA-Z]+)* - 零次或多次重复
    • : - a colon : - 一个冒号
    • [a-zA-Z]+ - one or more ASCII letters [a-zA-Z]+ - 一个或多个 ASCII 字母
  • \b - word boundary \b - 单词边界

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

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