Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "asdf","asdf"
- "", "asdf"
- "asdf", ""
- "adsf", "", "asdf"
- "asdf""asdf", "asdf"
- "asdf", """asdf"""
- "asdf", """"
- (?m)""(?![ t]*(,|$))
- (?m) // enable multi-line matching (^ will act as the start of the line and $ will act as the end of the line (i))
- "" // match two successive double quotes
- (?! // start negative look ahead
- [ t]* // zero or more spaces or tabs
- ( // open group 1
- , // match a comma
- | // OR
- $ // the end of the line or string
- ) // close group 1
- ) // stop negative look ahead
- orgTexts = [
- '"asdf","asdf"',
- '"", "asdf"',
- '"asdf", ""',
- '"adsf", "", "asdf"',
- '"asdf""asdf", "asdf"',
- '"asdf", """asdf"""',
- '"asdf", """"'
- ]
- orgTexts.each{|orgText|
- # Preprocessing - Eliminate spaces before and after comma
- # Here is needed if you may have spaces before and after a valid comma
- orgText = orgText.gsub(Regexp.new('" *, *"'), '","')
- # Detect valid character (non-quote and valid quote)
- resText = orgText.gsub(Regexp.new('([^"]|^"|"$|(?<=,)"|"(?=,)|(?<=\\)")'), '-')
- # resText = orgText.gsub(Regexp.new('([^"]|(^|(?<=,)|(?<=\\))"|"($|(?=,)))'), '-')
- # [^"] ===> A non qoute
- # | ===> or
- # ^" ===> beginning quot
- # | ===> or
- # "$ ===> endding quot
- # | ===> or
- # (?<=,)" ===> quot just after comma
- # "(?=,) ===> quot just before comma
- # (?<=\\)" ===> escaped quot
- # This part is to show the invalid non-escaped quots
- print orgText
- print resText.gsub(Regexp.new('"'), '^')
- # This part is to determine if there is non-escaped quotes
- # Here is the actual matching, use this one if you don't want to know which quote is un-escaped
- isMatch = ((orgText =~ /^([^"]|^"|"$|(?<=,)"|"(?=,)|(?<=\\)")*$/) != 0).to_s
- # Basicall, it match it from start to end (^...$) there is only a valid character
- print orgText + ": " + isMatch
- print
- print ""
- print ""
- }
- "asdf","asdf"
- -------------
- "asdf","asdf": false
- "","asdf"
- ---------
- "","asdf": false
- "asdf",""
- ---------
- "asdf","": false
- "adsf","","asdf"
- ----------------
- "adsf","","asdf": false
- "asdf""asdf","asdf"
- -----^^------------
- "asdf""asdf","asdf": true
- "asdf","""asdf"""
- --------^^----^^-
- "asdf","""asdf""": true
- "asdf",""""
- --------^^-
- "asdf","""": true
- ".*"(n|(".*",)*)
- ^("[^"]*"s*,s*)*"[^"]*""[^"]*"
- (^|rn)("[^rn"]*"s*,s*)*"[^rn"]*""[^rn"]*"
- "(?:[^",\]*|\.)*(?:""(?:[^",\]*|\.)*)+"
- :%s/v("(,)@!)&(,@<!")&("(n)@!)&(^@<!")//gc
- : - start the vim command
- % - scope of the command is the whole file
- s - search and replace
- / - start of search pattern
- v - simple regex syntax (rather than vim style)
- (
- " - double quote
- (,) - comma
- @! - not followed by
- )
- & - and
- (
- , - comma
- @<!- does not precedes
- " - double quote
- )
- & - and
- (
- " - double quote
- (n) - line end
- @! - not followed by
- )
- & - and
- (
- ^ - line beginning
- @<! - does not precedes
- " - double quote
- )
- / - end of search pattern and start of replace pattern
- - replace with nothing (delete)
- / - end of replace pattern
- g - apply to all the matches
- c - confirm with user for every replacement
Add Comment
Please, Sign In to add comment