如何在一个字符串中搜索多个字符串? [英] How to search multiple strings in a string?

查看:363
本文介绍了如何在一个字符串中搜索多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要像这样的字符串这是一个测试字符串 包含任何字符串列表项 {, dog, string, bark}

I want to check in a powerquery new column if a string like "This is a test string" contains any of the strings list items {"dog","string","bark"}.

我已经尝试过 Text.PositionOfAny(这是一个测试字符串,{ dog, string, bark}) ,但该函数仅接受单字符值

I already tried Text.PositionOfAny("This is a test string",{"dog","string","bark"}), but the function only accepts single-character values

Expression.Error: The value isn't a single-character string.

有什么解决方案吗?

推荐答案

在这种情况下,您需要合并一些M 库函数在一起。

This is a case where you'll want to combine a few M library functions together.

您将要使用 Text.Contains 针对列表多次,这对于 List.Transform 是一个很好的例子。 List.AnyTrue 会告诉您是否有任何字符串匹配。

You'll want to use Text.Contains many times against a list, which is a good case for List.Transform. List.AnyTrue will tell you if any string matched.

List.AnyTrue(List.Transform({"dog","string","bark"}, (substring) => Text.Contains("This is a test string", substring)))

如果您希望有 Text.ContainsAny 函数,则可以编写!

If you wished that there was a Text.ContainsAny function, you can write it!

let
    Text.ContainsAny = (string as text, list as list) as logical =>
        List.AnyTrue(List.Transform(list, (substring) => Text.Contains(string, substring))),
    Invoked = Text.ContainsAny("This is a test string", {"dog","string","bark"})
in
    Invoked

这篇关于如何在一个字符串中搜索多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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