正则表达式匹配字符序列(Regex matching sequence of characters)

编程入门 行业动态 更新时间:2024-10-27 20:26:10
正则表达式匹配字符序列(Regex matching sequence of characters)

我有一个测试字符串,例如: The Sun and the Moon together, forever

我希望能够输入几个字符或单词,并且如果字符以正确的顺序出现在一起,即使缺少单词,也能匹配此字符串。 例如,以下搜索词应该全部匹配该字符串:

The Moon Sun tog Tsmoon The get ever

我应该使用什么样的正则表达式? 我应该补充一点,提供的测试字符串在应用程序中将是动态的,所以我希望能够使用基于搜索字符串的模式。

I have a test string such as: The Sun and the Moon together, forever

I want to be able to type a few characters or words and be able to match this string if the characters appear in the correct sequence together, even if there are missing words. For example, the following search word(s) should all match against this string:

The Moon Sun tog Tsmoon The get ever

What regex pattern should I be using for this? I should add that the supplied test strings are going to be dynamic within an app, and so I'd like to be able to use a pattern based on the search string.

最满意答案

从你的例子中,你可以显示部分单词( T ),忽略大小写( s , m )并允许每个输入的字符之间的任何内容。 所以作为第一次尝试,您可以:

设置忽略大小写选项 在每个章节输入之间插入正则表达式以匹配零个或多个任何内容。 您可以选择是否匹配最短或最长的运行。

试试看,如果你NSRegularExpression ,请阅读NSRegularExpression的文档,看看它是如何发展的。 如果您遇到问题,请提出一个显示您的代码和RE的新问题,并解释发生了什么/不按预期工作。

HTH

From your example Tsmoon you show partial words (T), ignoring case (s, m) and allow anything between each entered character. So as a first attempt you can:

Set the ignore case option Between each chapter input insert the regular expression to match zero or more of anything. You can choose whether to match the shortest or longest run.

Try that, reading the documentation for NSRegularExpression if you're stuck, and see how it goes. If you get stuck ask a new question showing your code and the RE constructed and explain what happens/doesn't work as expected.

HTH

更多推荐

本文发布于:2023-07-28 08:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1303062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   字符   正则表达式   Regex   sequence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!