正则表达式提取搜索短语中的搜索词
我有以下搜索短语,
- 我就需要提取ABC XYZ
- Mobile Accessories
- Samsung 250
只要它们以任何顺序出现在字符串中, 。该应用程序是 C# .Net。
Search Phrase
__________________________________________________________
ABC XYZ
ABC XYZ category:"Mobile Accessories"
category:"Mobile Accessories" ABC XYZ
ABC XYZ Model:"Samsung 250"
Model:"Samsung 250" ABC XYZ
ABC XYZ category:"Mobile Accessories" Model:"Samsung 250"
Model:"Samsung 250" category:"Mobile Accessories" ABC XYZ
category:"Mobile Accessories" Model:"Samsung 250" ABC XYZ
__________________________________________________________
提前致谢。
实施例1 输入 - ABC XYZ 类别:“移动配件” 输出 - ABC XYZ 和移动配件
示例 2 输入 - 型号:“Samsung 250”类别:“手机配件” ABC XYZ 输出 - Samsung 250、移动配件和 ABC XYZ
示例 3 输入 - ABC XYZ 输出 - ABC XYZ
示例 4 输入 - 型号:“Samsung 250”ABC XYZ 输出 - Samsung 250 和 ABC XYZ
I have the following search phrase and I need to extract
- ABC XYZ
- Mobile Accessories
- Samsung 250
whenever they occur in the string in any order. The application is C# .Net.
Search Phrase
__________________________________________________________
ABC XYZ
ABC XYZ category:"Mobile Accessories"
category:"Mobile Accessories" ABC XYZ
ABC XYZ Model:"Samsung 250"
Model:"Samsung 250" ABC XYZ
ABC XYZ category:"Mobile Accessories" Model:"Samsung 250"
Model:"Samsung 250" category:"Mobile Accessories" ABC XYZ
category:"Mobile Accessories" Model:"Samsung 250" ABC XYZ
__________________________________________________________
Thanks in advance.
Example 1
Input - ABC XYZ category:"Mobile Accessories"
Output - ABC XYZ and Mobile Accessories
Example 2
Input - Model:"Samsung 250" category:"Mobile Accessories" ABC XYZ
Output - Samsung 250, Mobile Accessories and ABC XYZ
Example 3
Input - ABC XYZ
Output - ABC XYZ
Example 4
Input - Model:"Samsung 250" ABC XYZ
Output - Samsung 250 and ABC XYZ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实想查找显式字符串,则 IndexOf< /a> 方法适合您(例如:s.IndexOf("ABC XYZ"))。
您显示的语法看起来有点像 field:"value" 语法,所以也许您想要一个像 "([az]+):\"([^"]+)\"" 这样的正则表达式(它应该匹配字段和成对的值)。
如果这不是你想要的,抱歉,但问题有点模糊。
If you're literally trying to find explicit strings, the IndexOf method will work for you (EG: s.IndexOf("ABC XYZ")).
The syntax you show looks kind of like a field:"value" syntax though, so perhaps you want a regex like "([a-z]+):\"([^"]+)\"" (Which should match out field and value in pairs).
If that's not what you're after sorry, but the question is a bit vague.
至于模型和类别,您可以使用类似的方式捕获它们:
这会搜索字符串
category:"
,后跟您的类别(假设可以更改,后跟另一个"@"category:""([^""]*)"""
。类似地,您可以提取模型:
Model:"([^"]*)"
。不确定其余的,但如果删除这两个,则留下自由字符串。
As for Model and Category, you can capture them using something like that:
This searches for the string
category:"
followed by a your category (which assumbly can change, followed by another"
. Of course, in c# this should be escaped:@"category:""([^""]*)"""
.Similarity, you can extract the Model:
Model:"([^"]*)"
.Not sure about the rest, but if you remove these two, you are left with the free string.
看起来您想从同一个字符串中提取一些不同的模式。一
方法是找到每个匹配项,然后将其从工作字符串中删除。
示例:
无论字符串的格式如何,这都将提取类别、型号和名称。您应该注意格式错误的字符串,例如:
将返回:
It seems like you want to extract a few different patterns from the same string. One
approach would be to find each match and then remove it from your working string.
Example:
This will extract the Category, Model and Name regardless of the format of the string. You should note that malformed strings such as:
Will return: