Guest User

Untitled

a guest
Dec 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. "asdf","asdf"
  2. "", "asdf"
  3. "asdf", ""
  4. "adsf", "", "asdf"
  5.  
  6. "asdf""asdf", "asdf"
  7. "asdf", """asdf"""
  8. "asdf", """"
  9.  
  10. (?m)""(?![ t]*(,|$))
  11.  
  12. (?m) // enable multi-line matching (^ will act as the start of the line and $ will act as the end of the line (i))
  13. "" // match two successive double quotes
  14. (?! // start negative look ahead
  15. [ t]* // zero or more spaces or tabs
  16. ( // open group 1
  17. , // match a comma
  18. | // OR
  19. $ // the end of the line or string
  20. ) // close group 1
  21. ) // stop negative look ahead
  22.  
  23. orgTexts = [
  24. '"asdf","asdf"',
  25. '"", "asdf"',
  26. '"asdf", ""',
  27. '"adsf", "", "asdf"',
  28. '"asdf""asdf", "asdf"',
  29. '"asdf", """asdf"""',
  30. '"asdf", """"'
  31. ]
  32.  
  33. orgTexts.each{|orgText|
  34. # Preprocessing - Eliminate spaces before and after comma
  35. # Here is needed if you may have spaces before and after a valid comma
  36. orgText = orgText.gsub(Regexp.new('" *, *"'), '","')
  37.  
  38. # Detect valid character (non-quote and valid quote)
  39. resText = orgText.gsub(Regexp.new('([^"]|^"|"$|(?<=,)"|"(?=,)|(?<=\\)")'), '-')
  40. # resText = orgText.gsub(Regexp.new('([^"]|(^|(?<=,)|(?<=\\))"|"($|(?=,)))'), '-')
  41. # [^"] ===> A non qoute
  42. # | ===> or
  43. # ^" ===> beginning quot
  44. # | ===> or
  45. # "$ ===> endding quot
  46. # | ===> or
  47. # (?<=,)" ===> quot just after comma
  48. # "(?=,) ===> quot just before comma
  49. # (?<=\\)" ===> escaped quot
  50.  
  51. # This part is to show the invalid non-escaped quots
  52. print orgText
  53. print resText.gsub(Regexp.new('"'), '^')
  54.  
  55. # This part is to determine if there is non-escaped quotes
  56. # Here is the actual matching, use this one if you don't want to know which quote is un-escaped
  57. isMatch = ((orgText =~ /^([^"]|^"|"$|(?<=,)"|"(?=,)|(?<=\\)")*$/) != 0).to_s
  58. # Basicall, it match it from start to end (^...$) there is only a valid character
  59.  
  60. print orgText + ": " + isMatch
  61. print
  62. print ""
  63. print ""
  64. }
  65.  
  66. "asdf","asdf"
  67. -------------
  68. "asdf","asdf": false
  69.  
  70.  
  71. "","asdf"
  72. ---------
  73. "","asdf": false
  74.  
  75.  
  76. "asdf",""
  77. ---------
  78. "asdf","": false
  79.  
  80.  
  81. "adsf","","asdf"
  82. ----------------
  83. "adsf","","asdf": false
  84.  
  85.  
  86. "asdf""asdf","asdf"
  87. -----^^------------
  88. "asdf""asdf","asdf": true
  89.  
  90.  
  91. "asdf","""asdf"""
  92. --------^^----^^-
  93. "asdf","""asdf""": true
  94.  
  95.  
  96. "asdf",""""
  97. --------^^-
  98. "asdf","""": true
  99.  
  100. ".*"(n|(".*",)*)
  101.  
  102. ^("[^"]*"s*,s*)*"[^"]*""[^"]*"
  103.  
  104. (^|rn)("[^rn"]*"s*,s*)*"[^rn"]*""[^rn"]*"
  105.  
  106. "(?:[^",\]*|\.)*(?:""(?:[^",\]*|\.)*)+"
  107.  
  108. :%s/v("(,)@!)&(,@<!")&("(n)@!)&(^@<!")//gc
  109.  
  110. : - start the vim command
  111. % - scope of the command is the whole file
  112. s - search and replace
  113. / - start of search pattern
  114. v - simple regex syntax (rather than vim style)
  115. (
  116. " - double quote
  117. (,) - comma
  118. @! - not followed by
  119. )
  120. & - and
  121. (
  122. , - comma
  123. @<!- does not precedes
  124. " - double quote
  125. )
  126. & - and
  127. (
  128. " - double quote
  129. (n) - line end
  130. @! - not followed by
  131. )
  132. & - and
  133. (
  134. ^ - line beginning
  135. @<! - does not precedes
  136. " - double quote
  137. )
  138. / - end of search pattern and start of replace pattern
  139. - replace with nothing (delete)
  140. / - end of replace pattern
  141. g - apply to all the matches
  142. c - confirm with user for every replacement
Add Comment
Please, Sign In to add comment