Skip to content
Advertisement

split queries with regex

#i am using regex to split this but i am getting wrong results.

import re

queries ="""
INSERT ignore into persons VALUES (15,'Tom D.', 'Erilchsen', 
'Skagen 21', 'Erlangen');
select * from persons;
"""

regex = "[;!]+?"
y = re.split(regex ,queries)
print(y)
print(len(y))
print(y[0])

output:

["nINSERT ignore into persons VALUES (15,'Tom D.', 'Erilchsen', n'Skagen 21', 'Erlangen')", 'nselect * from persons', 'n']
3

Advertisement

Answer

You don’t actually need a regex for this:

queries ="""
INSERT ignore into persons VALUES (15,'Tom D.', 'Erilchsen', 
'Skagen 21', 'Erlangen');
select * from persons;
"""

y = list(filter(None, queries.replace("n", "").split(";")))
print(y)
print(len(y))
print(y[0])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement