Match everything after the 8th comma

Viewed 74

I'm parsing a raw log file and have to resort to regex because the comma delimiter shows up in a field.

20200914 01:38:16,ip-XX-YY-1-202,user,10.0.0.0,978,108421,QUERY,db,'select some random query, that has, commas in it, sometimes and not always quotes either..'

This is a maria DB audit log - I need to select the Database query which is everything after the 8th comma. The output for the above example would be

`'select some random query, that has, commas in it, sometimes and not always quotes either..'`

I'm processing using pandas and .str.extract. I have a hacky version that matches everything before the 8th comma, but not sure how to get what's after it.

1 Answers

One way without regex is to use str.split:

s.split(",", 8)[-1]

Output:

"'select some random query, that has, commas in it, sometimes and not always quotes either.'"

Benchmark:

%timeit s.split(",", 8)[-1]

# 511 ns ± 46.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit re.search("^(?:[^,]*,){8}(.*)$", s).group(1)
# @41686d6564's comment
# 1.52 µs ± 121 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Related