skip to Main Content

As the title says I need a Regex expression that will match a line that does not end in a certain character(") and then to substitute the entire line and append at end.
There are lines in my file that should end with " but it is missing so I need to find these lines and add the missing "
Using Regex and VS Code find & replace this expression

[^n"/n](n|$) 

finds all lines of this sort but I cannot append as it gets rid of the last character of the string when trying to replace with

$1 "

4

Answers


  1. Put VS Code find-and-replace in regex mode. Put .*[^"]$ in the search field, and put $0" in the replace field. "$0" in the replace field represents the whole content that was matched for each match.

    Login or Signup to reply.
  2. You are missing the last character as you match it, but only use the capture group in the replacement with $1

    You can match [^"]$ and use the full match in the replacement like $0 "

    Login or Signup to reply.
  3. Instead of searching the lines that don’t end with ", you can search all end of the line without distinction with an optional ":

    • search: "?$
    • replacement: "

    if a line already has a double quote, this one is replaced with a double quote. If a line doesn’t have a double quote, a double quote is added.

    Notice: don’t try to do that with regex101 because a line that ends with " is matched twice (at the double quote position and at the end of line position), it only works in VScode.

    Login or Signup to reply.
  4. You don’t need to select anything really.

    Find: (?<!"|^s*)$
    Replace: "

    The regex just checks that the end of the line $ is not preceded by a " or
    is not a blank line with only whitespace on it.

    See regex101 demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search