[英]Regex matching sequence of characters
I have a test string such as: The Sun and the Moon together, forever
我有一个测试字符串,例如:
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
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.
我应该补充说,提供的测试字符串将在应用程序内是动态的,因此我希望能够使用基于搜索字符串的模式。
From your example Tsmoon
you show partial words ( T
), ignoring case ( s
, m
) and allow anything between each entered character. 在示例
Tsmoon
您将显示部分单词( T
),忽略大小写( s
, m
),并在每个输入的字符之间允许任何内容。 So as a first attempt you can: 因此,您可以尝试以下操作:
Try that, reading the documentation for NSRegularExpression
if you're stuck, and see how it goes. 尝试一下,如果遇到问题,请阅读
NSRegularExpression
的文档,并查看其进展。 If you get stuck ask a new question showing your code and the RE constructed and explain what happens/doesn't work as expected. 如果您陷入困境,请提出一个新问题,以显示您的代码和可重构的RE,并说明会发生什么/不起作用。
HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.