Python regular expression simple OR condition doesn't work?
Question: I am trying to write a simple regular expression in Python that recognizes
either a comma or a newline, to be used as a delimiter and split() text.

I have tried the following:

    
    
    delim = r'[,\n]'
    delim = r'[\n,]'
    delim = r',|\n'
    delim = r'[,|\n]'
    delim = r'(,\n)'
    

None of these work. The split() works fine if I make it just one or the other,
such as...

    
    
    delim = r','
    delim = r'\n'
    

But not if I try and do both.

What am I missing here?

Thank you for your input.

Whole code:

    
    
        data = "abc,def\nghi"
        delim = r'[,\n]'
        values = data.split(delim)
        print(values)
    


Answer: You are using `str.split()`, which doesn't take a regex as an argument.

Try using `re.compile` on your regex string, and then using that object for
the split:

    
    
    import re
    
    data = "abc,def\nghi"
    delim = re.compile(r'[,\n]')
    values = delim.split(data)
    print(values)
    

Yields:

    
    
    ['abc', 'def', 'ghi']
    

