Search code examples
regexsublimetext

How to get values separated by comma discarding a string before it


Considering the lines

animal1=fish,dog,lion,duck,bee,rabbit,cat    
animal2=turkey,dolphin,bear,goat,tiger    
animal3=elephant,butterfly,alpaca,chicken,horse

We can get the separated words by comma using for example:

(.*?)(?:,|$)

and we'll get (|#| meaning each selection separation):

animal1=fish,|#|dog,|#|lion,|#|duck,|#|bee,|#|rabbit,|#|cat    
animal2=turkey,|#|dolphin,|#|bear,|#|goat,|#|tiger    
animal3=elephant,|#|butterfly,|#|alpaca,|#|chicken,|#|horse

How do I manage to get the separated words, with the multiple selection in each word, discarding "animal1=", "animal2=" and "animal3="?

fish,|#|dog,|#|lion,|#|duck,|#|bee,|#|rabbit,|#|cat    
turkey,|#|dolphin,|#|bear,|#|goat,|#|tiger    
elephant,|#|butterfly,|#|alpaca,|#|chicken,|#|horse

What I could get so far is to use the negative lookbehind, but it 'anchors' to first occurrence:

(?<=animal\d=).[^,\s]*

Using Sublime Text, simple Find.


Solution

  • The following should work on Sublime text for highlighting all the words you want to match:

    (?!.*=)\w+
    

    This matches any word (which implicitly rules out the comma separators) after looking ahead and asserting that there is no = term. This means that the leading key= will not be matched.