Home > Back-end >  Notepad all instances of character under a condition
Notepad all instances of character under a condition

Time:02-04

This is my text

BROKEN This is a "sentence".
This sentence is an actual normal sentence.

I wish to replace/filter the quotation marks out of every line that has the word BROKEN in it I thought this would be simple but I couldn't do it my regex

(?=BROKEN)"

could I get some help?

CodePudding user response:

  • Ctrl H
  • Find what: (?:^.*?\bBROKEN\b|\G(?!^))[^"\r\n]*\K"
  • Replace with: LEAVE EMPTY
  • TICK Match case
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

(?:             # non capture group
  ^               # beginning of line
    .*?             # 0 or more any character but newline
    \bBROKEN\b      # literally
  |               # OR
    \G              # restart from last match position
    (?!^)           # not at the beginning of line
)               # end group
[^"\r\n]*       # 0 or more any character that is not a quote or linebreak
\K              # forget all we have seen until this position
"               # quote

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

CodePudding user response:

If you also want to match double quotes before the word BROKEN, you can skip the whole line that does not contain the word.

Find what:

^(?!.*\bBROKEN\b).*\R?(*SKIP)(*F)|"

Replace with: (leave empty)

Explanation

  • ^ Start of string
  • (?!.*\bBROKEN\b) Negative lookahead, assert that the word BROKEN does not occur
  • .*\R?(*SKIP)(*F) Match the whole line including an optional newline and skip the match
  • | Or
  • " Match a double quote

See a enter image description here

After

enter image description here

  • Related