GregdeLima
GregdeLima

Reputation: 404

Regex for Last Two Quotes per Line

I'm using the following as part of SQL query and it got me hooked in trying to figure this out:

"ZENDCUST" as "End Customer ID",
"ZCENCSLS___T" as "End Customer Desc",
"ZCENCSLS__0CUST_GROUP" as "Cust Grp-EC-MD ID",
"ZCENCSLS__0CUST_GROUP___T" as "Cust Grp-EC-MD Desc",
"ZENDCUST__0CITY" as "End Customer City",

Is there a RegEx that can get the third & fourth double quote marks? i.e. line 1 becomes "ZENDCUST" as 'End Customer ID',

This would be repeated across more than the 5 example rows.

I was able to use \"(?!\sas) but this wasn't able to omit the first quote.

https://regexr.com/40jhr

Upvotes: 0

Views: 242

Answers (2)

Valdi_Bo
Valdi_Bo

Reputation: 31011

Maybe the solution for you is not matching 3rd and 4th double quote, but the last pair of double quotes in a row (and what is between them).

The regex to find them is:

"([^"]+)"(?!.*")

Details:

  • " - A double quote.
  • ([^"]+) - A sequence of chars other than a double quote, as a capturing group.
  • " - The second double quote.
  • (?!.*") - Negative lookahead for a double quote, somewhere further in the current row. This guarantees that what has been captured before is the last pair of double quotes.

If you e.g. substitite each match with '\1', you change the double quotes in question into single ones.

For a working example see https://regex101.com/r/a7GyGS/1

Upvotes: 2

revo
revo

Reputation: 48751

If the tool or language that you are working with supports lookaheads in its set of regular expressions then you would be able to use a positive lookahead. You should enable m flag too:

"(?=[^"]*(?:"[^"]*)?$)

See live demo here

Breakdown:

" # Match a double quotation mark
(?= # Start of a positive lookahead
    [^"]* # Match anything but a "
    (?: # Start of a non-capturing group
        "[^"]* # Match one " and anything but a "
    )? # End of non-capturing group, optional
    $ End of line
) # End of positive lookahead

Upvotes: 1

Related Questions