0

I currently use this regex to count the number of commas in a line of text:

^([^,]*,){9}[^,]*$

Here is a sample message from our test server:

2018-06-25T19:44:31.187Z,MAIL\Outbound Proxy Frontend MAIL,08D5D14527D3D0F3,1,192.168.20.1:717,192.168.20.23:28050,>,"220 MAIL.potato.ca Microsoft ESMTP MAIL Service ready at Mon, 25 Jun 2018 15:44:30 -0400",

Now this works but it also counts the comma within the quotes (near the end). I would need it to work with detecting the 8 commas, thus ignoring any inside quoted "" text. Can't get my head around it so any ideas how I could do this?

To provide context, this is to parse logs correctly from a mail servers and with my current setup if an email has one or multiple commas in the subject it breaks my regex detection...

NB. This isn't in a language but the regex engine behind is java based.

Not sure why flagged as duplicate. The linked question is for code based extraction and not count.

Zoe
  • 27,060
  • 21
  • 118
  • 148
JulioQc
  • 310
  • 1
  • 4
  • 20
  • Can you use a CSV parser? – anubhava Jun 25 '18 at 20:16
  • 1
    Try [`^((?:"[^"]*"|[^,])*,){9}(?:"[^"]*"|[^,])*$`](https://regex101.com/r/RzlTY4/1) to validate 10 fields. It would be better if you could use a CSV parser. – Wiktor Stribiżew Jun 25 '18 at 20:20
  • I have a CSV parser to extract fields but I must tell the system what logs to parse and the most efficient way is to count the "fields". – JulioQc Jun 25 '18 at 20:35
  • And your suggestions works for either 8 or 9 fields. So i suppose it works to count without including inside quotes and also including those? – JulioQc Jun 25 '18 at 20:42
  • @anubhava why the flag? did you even read the question? – JulioQc Jun 25 '18 at 20:46
  • Presumably I did. Did you read the dupe question and answers? – anubhava Jun 25 '18 at 20:52
  • Yes I did and it's not what I asked. Anyways it's not important. It just feels like this was tagged without even being read. – JulioQc Jun 25 '18 at 20:57
  • [The regex in the accepted answer over there matches commas](https://stackoverflow.com/a/1757107/2891664), so it can be used to count them. See https://ideone.com/vsdtbM for example. – Radiodef Jun 25 '18 at 21:19
  • again, that's to extract not to count and it's code based. I came across those but couln't get my head around them to count instead of capture so here I am, – JulioQc Jun 26 '18 at 12:38

0 Answers0