我有一个搜索要求。例如,我想在大量内容中搜索单词"Microsoft Account“。在大文本中,它可能被定义为"Microsoft_Account“或"Microsoft-Account”。我的搜索逻辑也应该识别上面的单词。有没有办法用正则表达式来实现呢?(可以通过拆分和循环搜索来完成,但如果有任何使用正则表达式的解决方案就更好了)
发布于 2018-11-23 19:39:13
如果您只需要regEx,那么它是:a[ -_]b
其中a和b是您搜索的两个部分
如果你需要一个算法:
您需要首先使用regex拆分器:[ -_]拆分您的搜索词(在许多语言中,此yourString.split(regex)),它将允许三个不同的字符。
在许多cas中,split返回一个字符串表。因此,您必须在表中查找以重新创建正则表达式。
算法
str = your_search_string
tab_string = str.split("[ -_]")
res = ""
foreach part in tab_string
res = res + part + "[ -_]"
endForeach
res = res[0 length-5] //to remove "[ -_]" at the end使用这个小算法,您将拥有以下示例:
str = "Microsoft Account"
tab_string = ["Microsoft", "Account"]
res = ""
forEach
| res = "Microsoft[ -_]"
| res = "Microsoft[ -_]Account[ -_]"
EndforEach
res = "Microsoft[ -_]Account"https://stackoverflow.com/questions/53445534
复制相似问题