Regex 使用正则表达式删除行中的重复单词

Regex 使用正则表达式删除行中的重复单词,regex,Regex,我想删除一行中重复的单词 例如: arraythis1, XdashedSmall, Small, Medium, Large, XdashedLarge, XdashedSmall, Small, Medium, Large, XdashedLarge 我想删除所有重复项,将行转换为: arraythis1, XdashedSmall, Small, Medium, Large 我的正则表达式是这样的:\w(\D+)(:,\s++\1\b,)+/gm,请参见。我不确定您的确切输入,但在这个

我想删除一行中重复的单词

例如:

arraythis1, XdashedSmall, Small, Medium, Large, XdashedLarge, XdashedSmall, Small, Medium, Large, XdashedLarge
我想删除所有重复项,将行转换为:

arraythis1, XdashedSmall, Small, Medium, Large

我的正则表达式是这样的:
\w(\D+)(:,\s++\1\b,)+/gm
,请参见。

我不确定您的确切输入,但在这个示例中,如果您只想删除第一个“arraythis1”,您可以使用以下正则表达式:

   ^[^\,]*
  • 第一个胡萝卜(“^”)表示“从队伍的最前面开始”
  • 方括号(“[]”)表示匹配列表中不存在的单个字符(在方括号中)
  • 我通过在列表前面使用另一个胡萝卜来反转方括号(而不是匹配单个字符,不匹配单个字符)
  • 最后,我使用星号(“*”)确保捕获所有不是逗号的字符
最后,要生成最后一个正则表达式,需要删除剩余的空间

尝试此操作。替换为
空字符串
。请参阅演示


我想你应该试试这个

var words = new HashSet<string>();
string text = "arraythis1, XdashedSmall, Small, Medium, Large, XdashedLarge, XdashedSmall, Small, Medium, Large, XdashedLarge";
text = Regex.Replace(text, "\\w+", m =>
                 words.Add(m.Value.ToUpperInvariant())
                     ? m.Value
                     : String.Empty);
var words=newhashset();
string text=“arraythis1,XdashedSmall,Small,Medium,Large,XdashedLarge,XdashedSmall,Small,Medium,Large,XdashedLarge”;
text=Regex.Replace(文本“\\w+”,m=>
words.Add(m.Value.toupper不变量())
?m.值
:String.Empty);

您的实际输入和预期输出是什么?但输出中重复的是小的/不要删除
arraythis1,
查看我的输出,这将是result@j2query你用什么语言?这是不应该使用正则表达式的内容。您可能希望使用
(\b[^\n,]+),(?=.*\b\1\b)
,以避免将新行添加到要删除的重复项中。
(\b[^,]+),(?=.*\b\1\b)
var words = new HashSet<string>();
string text = "arraythis1, XdashedSmall, Small, Medium, Large, XdashedLarge, XdashedSmall, Small, Medium, Large, XdashedLarge";
text = Regex.Replace(text, "\\w+", m =>
                 words.Add(m.Value.ToUpperInvariant())
                     ? m.Value
                     : String.Empty);