正则表达式或Kotlin的when语句中的通配符? [英] Regex or Wildcard in Kotlin's when statement?

查看:164
本文介绍了正则表达式或Kotlin的when语句中的通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Kotlin上开发RESTful应用程序,对于路由器,我使用的是when语句,因为它是最易读且美观的条件.

I'm working on a RESTful app in Kotlin and for the router, I'm using a when statement, as it's the most readable and good looking conditional.

在字符串的when语句中是否可以使用正则表达式或通配符?

Is there a way to use Regex or a wildcard in the when statement for a string?

(这样,诸如"/article/get/"之类的URI都将传递到同一控制器)

(So that URIs like "/article/get/" would all be passed to the same controller)

我的路由器的结构如下:

The structure of my router is as follows:

when(uri) {
    "some/url" -> return SomeController(config).someAction(session)
}

推荐答案

是.

import kotlin.text.regex

val regex1 = Regex( /* pattern */ )
val regex2 = Regex( /* pattern */ )
/* etc */

when {
    regex1.matches(uri) -> /* do stuff */
    regex2.matches(uri) -> /* do stuff */
    /* etc */
}

您还可以使用 containsMatchIn

You could also use containsMatchIn if that suits your needs better than matches.

说明:

when 语句的测试表达式是可选的.如果不包含测试表达式,则when语句的功能类似于if-else if链,其中 whenEntry 的noreferrer> whenCondition 独立评估为布尔值.

The test expression of a when statement is optional. If no test expression is included, then the when statement functions like an if-else if chain, where the whenCondition of each whenEntry shall independently evaluate to a boolean.

所以我考虑了一段时间,然后想出了一种可能更接近您想要的方法.

So I thought about it for awhile, and I came up with a different approach that might be closer to what you want.

import kotlin.text.regex

when (RegexWhenArgument(uri)) {
    Regex(/* pattern */) -> /* do stuff */
    Regex(/* pattern */) -> /* do stuff */
    /* etc */
}

RegexWhenArgument的最低定义为:

class RegexWhenArgument (val whenArgument: CharSequence) {
    operator fun equals(whenEntry: Regex) = whenEntry.matches(whenArgument)
    override operator fun equals(whenEntry: Any?) = (whenArgument == whenEntry)
}

这种方法使您尽可能接近自变量式" when表达式语法.我认为它尽可能精简易读(假设您在其他地方定义了RegexWhenArgument类).

This approach lets you get as close as possible to the "argument-ful" when expression syntax. I think it's about as streamlined and readable as it can be (assuming that you define the RegexWhenArgument class elsewhere).

这种方法使用了类似于访问者设计模式的东西,并结合了Kotlin的运算符重载,以重新定义构成when表达式参数和whenEntry之间的匹配项"的内容.如果您确实愿意,我想您可以进一步采取这种方法,并将RegexWhenArgument生成为通用的WhenArgumentWhenArgumentDecorator,这些通用的WhenArgumentWhenArgumentDecorator允许您在when表达式中为各种类型,而不仅仅是正则表达式.

This approach uses something similar to the visitor design pattern in combination with Kotlin's operator overloading to redefine what constitutes a "match" between a when expression argument and a whenEntry. If you really wanted to, I suppose you could take this approach a step further and generify RegexWhenArgument into a general-purpose WhenArgument and WhenArgumentDecorator that allows you to specify custom "match" criteria in a when expression for any sort of type, not just Regex.

这篇关于正则表达式或Kotlin的when语句中的通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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