regex - Deleting back to back substrings from a string but not all occurences - c# -


i have string like:

"water water asdf fdsa"

as strings like:

"water water asdf fdsa water"

i need remove first instance of back substrings these 2 cases become:

"water asdf fdsa" , "water asdf fdsa water"

what tried do:

        list<string> substrings = findsubstrings(returnstring);         ienumerable<string> duplicateitems = x in substrings                              group x x grouped                              grouped.count() > 1                              select grouped.key; 

so have seperate findsubstrings method returns list contains substrings original string. (from online found) way detect duplicates list of them placed in ienumerable.

is best way go like:

       (int = 0; < substrings.count; i++)         {            //if duplicateitems contains substring , substring[i+1] same item, remove it)         } 

the issue cant string duplicateitems if contains more 1 duplicate substring becuase there no indexing on ienumerable. ideas on best way this?

you can search using regex:

\b(\w+)\s+(?=\1) 

and replace empty string.

regex demo

(?=\1) positive lookahead makes sure there repeat of word being captured using (\w+).

code:

string repl = regex.replace(input, @"\b(\w+)\s+(?=\1)", ""); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -