Groovy正则表达式:在Switch语句中捕获组 [英] Groovy Regex: Capture group in Switch Statement

查看:120
本文介绍了Groovy正则表达式:在Switch语句中捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def vehicleSelection =汽车选择:丰田

switch(vehicleSelection){
case〜/ Car Selected:(。*)/:

println选择的车型为+ ?? [0] [1]





$ b

是否可以在没有的情况下提取Toyota定义一个新的( def )变量?

解决方案

Groovy添加到 Matcher 中的 lastMatcher 方法:

 import java.util.regex.Matcher 
$ b $ def vehicleSelection ='汽车选择:丰田

switch(vehicleSelection){$ b $选择车型:$ {Matcher.lastMatcher [0] [1]}
}


Given the following Groovy code switch statement:

def vehicleSelection = "Car Selected: Toyota"

switch (vehicleSelection) {
   case ~/Car Selected: (.*)/:

      println "The car model selected is "  + ??[0][1] 
}

Is it possible to extract the word "Toyota" without defining a new (def) variable?

解决方案

This is possible using the lastMatcher method added to Matcher by Groovy:

import java.util.regex.Matcher

def vehicleSelection = 'Car Selected: Toyota'

switch( vehicleSelection ) {
   case ~/Car Selected: (.*)/: 
     println "The car model selected is ${Matcher.lastMatcher[0][1]}"
}

这篇关于Groovy正则表达式:在Switch语句中捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆