正则表达式提取搜索短语中的搜索词

发布于 2024-08-12 07:20:44 字数 878 浏览 5 评论 0原文

我有以下搜索短语,

  1. 我就需要提取ABC XYZ
  2. Mobile Accessories
  3. 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

  1. ABC XYZ
  2. Mobile Accessories
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在你怀里撒娇 2024-08-19 07:20:45

如果您确实想查找显式字符串,则 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.

遗失的美好 2024-08-19 07:20:45

至于模型和类别,您可以使用类似的方式捕获它们:

category:"([^"]*)"

这会搜索字符串 category:" ,后跟您的类别(假设可以更改,后跟另一个 "@"category:""([^""]*)"""
类似地,您可以提取模型:Model:"([^"]*)"

不确定其余的,但如果删除这两个,则留下自由字符串。

As for Model and Category, you can capture them using something like that:

category:"([^"]*)"

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.

两仪 2024-08-19 07:20:45

看起来您想从同一个字符串中提取一些不同的模式。一
方法是找到每个匹配项,然后将其从工作字符串中删除。

示例:

String workingstring = "ABC XYZ category:\"Mobile Accessories\"";

Regex categoryMatch("category:\"([^\"]+)\"");
Regex modelMatch("model:\"([^\"]+)\"");

String category = categoryMatch.Match(workingstring);
String model = modelMatch.Match(workingstring);

workingstring = Regex.Replace(workingstring, categoryMatch, "");
workingstring = Regex.Replace(workingstring, modelMatch, "");

String name = workingstring; //I assume that the extra data is the name

无论字符串的格式如何,这都将提取类别、型号和名称。您应该注意格式错误的字符串,例如:

ABC Model:"Samsung 250" XYZ

将返回:

ABC  XYZ

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:

String workingstring = "ABC XYZ category:\"Mobile Accessories\"";

Regex categoryMatch("category:\"([^\"]+)\"");
Regex modelMatch("model:\"([^\"]+)\"");

String category = categoryMatch.Match(workingstring);
String model = modelMatch.Match(workingstring);

workingstring = Regex.Replace(workingstring, categoryMatch, "");
workingstring = Regex.Replace(workingstring, modelMatch, "");

String name = workingstring; //I assume that the extra data is the name

This will extract the Category, Model and Name regardless of the format of the string. You should note that malformed strings such as:

ABC Model:"Samsung 250" XYZ

Will return:

ABC  XYZ
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文