regex

2010-06-10 15 views
5

में डुप्लिकेट से छुटकारा पाने के लिए कैसे मान लीजिए कि मेरे पास एक स्ट्रिंग थी, "बिल्लियों बिल्लियों बिल्लियों और कुत्ते कुत्तों के कुत्ते।"regex

उस स्ट्रिंग को "बिल्लियों और कुत्तों" के साथ बदलने के लिए मैं नियमित रूप से किस अभिव्यक्ति का उपयोग करूंगा। यानी डुप्लिकेट को हटा रहा है। अभिव्यक्ति हालांकि केवल एक दूसरे के बाद अनुवर्ती डुप्लीकेट को हटा देना चाहिए। उदाहरण के लिए:

"बिल्लियों बिल्लियों बिल्लियों और कुत्तों कुत्ते कुत्तों और बिल्लियों बिल्लियों और कुत्तों कुत्ते"

वापसी होगी:

"बिल्लियों और कुत्तों और बिल्लियों और कुत्तों"

+0

बाहर चेक http://stackoverflow.com/questions/1058783/regular-expression-to-find-and-remove-duplicate-words यह आप कुछ संकेत पर दे सकता है आपका प्रश्न। –

उत्तर

2

$1 साथ (\w+)\s+\1 बदलें

इसे एक लूप में करें जब तक कि कोई और मिलान न मिले। global ध्वज की स्थापना उस में cats cats cats

\1 regex में पहली कब्जा कर लिया समूह की सामग्री को संदर्भित करता है तीसरे cats की जगह नहीं होगा, क्योंकि पर्याप्त नहीं है।

प्रयास करें:

str = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs"; 
str = Regex.Replace(str, @"(\b\w+\b)\s+(\1(\s+|$))+", "$1 "); 
Console.WriteLine(str); 
+0

मैं इस कोड का उपयोग कर रहा हूं: replacer = Regex.Replace (replacer, @ "([\\ n] +) [\ s +]? \ 1", string.Empty); लेकिन यह काम नहीं लग रहा है। यह rubular में काम करता है हालांकि http://www.rubular.com/r/Ey6wrLYXNw –

+0

@Emmanuel कोशिश करें 'str = Regex.Replace (str, @ "(\ w +) \ s + \ 1", "$ 1"); ' – Amarghosh

+0

यह नीचे मतदान क्यों किया गया था? – Amarghosh

1

कोई संदेह नहीं है तो किसी छोटे regex संभव है, लेकिन यह एक चाल करने के लिए लगता है: समाप्त नहीं

string somestring = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs"; 
Regex regex = new Regex(@"(\w+)\s(?:\1\s)*(?:\1(\s|$))"); 
string result = regex.Replace(somestring, "$1$2"); 

यह भी ध्यान में पिछले "कुत्ता" लेता है एक जगह के साथ।

+0

यह बहुत सी जगहों को हटा देगा: 'बिल्लियों बिल्लियों बिल्लियों और कुत्तों के कुत्ते कुत्तों और बिल्लियों बिल्लियों और कुत्तों के कुत्ते' बिल्लियों और कुत्तों और कुत्तों 'बन जाते हैं। यह भी बहुत मेल खाता है: 'सीडी पर माइकल बोल्टन' माइकल बोल्टन सीडी बन गया। कार्यालय अंतरिक्ष संदर्भ के बारे में खेद है। –

+0

अजीब, मैं उन त्रुटियों को पुन: उत्पन्न नहीं कर सकता। शायद मुझे फ्लेयर के कुछ और टुकड़े जोड़ना चाहिए:] –

+1

ओह, मुझे याद आया कि आप '$ 1 $ 2' के साथ बदल रहे हैं, इसलिए मैंने जो पहली समस्या देखी थी, वह वहां नहीं है। लेकिन माइकल बोल्टन अभी भी एक समस्या है। शायद कुछ सम्मोहन मदद करेगा (या शब्द सीमा \ \'' \ w' से पहले)। –

9
resultString = Regex.Replace(subjectString, @"\b(\w+)(?:\s+\1\b)+", "$1"); 

एक ही कॉल में सभी प्रतिस्थापन करेगा।

स्पष्टीकरण:

\b     # assert that we are at a word boundary 
        # (we only want to match whole words) 
(\w+)    # match one word, capture into backreference #1 
(?:    # start of non-capturing, repeating group 
    \s+    # match at least one space 
    \1    # match the same word as previously captured 
    \b    # as long as we match it completely 
)+     # do this at least once 
+0

टिम, आप एक रेगेक्स गुरु हैं। आदर करना! :) – Koen

+0

+1, क्योंकि अभिव्यक्ति काम करती है और इसके अलावा इसे समझाया जाता है। –

0

निम्नलिखित कोड का प्रयास करें।



using System; 
using System.Text.RegularExpressions;

namespace ConsoleApplication1 { /// <summary> ///
/// A description of the regular expression: ///
/// Match expression but don't capture it. [^|\s+] /// Select from 2 alternatives /// Beginning of line or string /// Whitespace, one or more repetitions /// [1]: A numbered capture group. [(\w+)(?:\s+|$)] /// (\w+)(?:\s+|$) /// [2]: A numbered capture group. [\w+] /// Alphanumeric, one or more repetitions /// Match expression but don't capture it. [\s+|$] /// Select from 2 alternatives /// Whitespace, one or more repetitions /// End of line or string /// [3]: A numbered capture group. [\1|\2], one or more repetitions /// Select from 2 alternatives /// Backreference to capture number: 1 /// Backreference to capture number: 2 ///
/// /// </summary> class Class1 { /// /// Point d'entrée principal de l'application. /// static void Main(string[] args) { Regex regex = new Regex( "(?:^|\s+)((\w+)(?:\s+|$))(\1|\2)+", RegexOptions.IgnoreCase | RegexOptions.Compiled ); string str = "cats cats cats and dogs dogs dogs and cats cats and dogs dogs"; string regexReplace = " $1";

Console.WriteLine("Before :" + str); str = regex.Replace(str,regexReplace); Console.WriteLine("After :" + str); } }

}

 संबंधित मुद्दे