0
STOPATDESK YES;
:: TXT "LCLLMT:29.4700";
:: TXT "LCLCURR;NON-USD";
:: TXT "CALLBK:3";
:: TXT "FFTRL:EUR-LIM;-TAP-5";

STOPATDESK YES; :: TXT "LCLLMT:29.4700"; :: TXT "LCLCURR;NON-USD"; :: TXT "CALLBK:3"; :: TXT "FFTRL:EUR-LIM;-TAP-5";

Could you please provide regex that will match semicolons but not within TXT "..."?

There were several useful questions on StackOverflow but I failed to compile working solution for my case
Regex for matching a character, but not when it's enclosed in square bracket
Regex for matching a character, but not when it's enclosed in quotes

Community
  • 1
  • 1
Mike
  • 18,562
  • 24
  • 92
  • 124

2 Answers2

2

You need a regex that matches any semicolon that is not followed by an odd number of quotes.

;(?![^"]*(([^"]*"[^"]*"){2})*[^"]*"[^"]*$)

The tricky part is to find the negative lookahead (?![^"]*(([^"]*"[^"]*"){2})*[^"]*"[^"]*$):

  • [^"]* match any text before the first " after ;
  • (([^"]*"[^"]*"){2})* match any even number of quotes with text inside
  • [^"]*"[^"]*$ match the last quote

If all the above conditions are matched, then an odd number of " is found after ;. That implies that the ; is inside two " and therefore it's not a valid ;.

Regex: https://regex101.com/r/dG6cC1/1

Java demo: https://ideone.com/OuAaA5

Tobías
  • 5,922
  • 3
  • 33
  • 59
0

You can also try with:

"[^"]*"|(;)

DEMO

which will match quotes or separate semicolons, then get separate semicolons with group(1). However the unbalanced quoting sings would couse a problem. Or, if whole file is formated as your example (semicolons in quotation are preceded and followed by another character, not whitespace), you can try with:

;(?=\s|$)

DEMO

It works with example above.

m.cekiera
  • 5,337
  • 5
  • 20
  • 35