了解匹配运算符
Understanding the match operator
您好,我对匹配运算符的用法感到困惑。我遇到了一段代码,看起来与文档中的解释完全不同:https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#match
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
请帮助理解匹配语法的含义
好问题! match
关键字在 DataWeave 中有两个用途,这取决于它的位置。匹配用于正则表达式或模式匹配。
match
正则表达式
如果match
左边(lhs)是字符串,rhs是正则表达式,那么会按照下面的docs进行操作。基本上,它正在进行正则表达式匹配:
Match returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex.
match
用于模式匹配
如果 match
在 lhs 上有任何计算结果为一个值(即,不计算为一个函数)并且在 rhs 上有一个开括号,match
现在正在进行模式匹配。您可以找到 here. I cover this pretty extensively in the talk I did, you can find the slides for that here.
的文档
对于您提供的示例(格式很好,顺便说一句):
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
match
正在检查其输入 x
是否为 null
、A、B、J、K 或 L。如果匹配其中任何一个,DW 将评估箭头的右侧是什么,然后立即 return。如果没有匹配项,它将 return default
.
箭头右侧的内容
您好,我对匹配运算符的用法感到困惑。我遇到了一段代码,看起来与文档中的解释完全不同:https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#match
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
请帮助理解匹配语法的含义
好问题! match
关键字在 DataWeave 中有两个用途,这取决于它的位置。匹配用于正则表达式或模式匹配。
match
正则表达式
如果match
左边(lhs)是字符串,rhs是正则表达式,那么会按照下面的docs进行操作。基本上,它正在进行正则表达式匹配:
Match returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex.
match
用于模式匹配
如果 match
在 lhs 上有任何计算结果为一个值(即,不计算为一个函数)并且在 rhs 上有一个开括号,match
现在正在进行模式匹配。您可以找到 here. I cover this pretty extensively in the talk I did, you can find the slides for that here.
对于您提供的示例(格式很好,顺便说一句):
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
match
正在检查其输入 x
是否为 null
、A、B、J、K 或 L。如果匹配其中任何一个,DW 将评估箭头的右侧是什么,然后立即 return。如果没有匹配项,它将 return default
.