user1874594
user1874594

Reputation: 2483

find a string and replace a pattern before it - Notepad++

I am trying to acheive this using notepad++

Input file has pattern
< a code snippet statement - this is variabe , followed by.. > ( Quality in parenthesis )

-- "command xyz d 
   command rererere 
   command ;" ( fair quality)
---- "command xyz d 
   command rereredfdfdre 
   command ffdfdfdf ;" ( good quality)
---- "command xffddyz d 
   command rerfdferere 
   command ;" ( good quality)
----"command fdfdfxyz d 
   command refffdrerere 
   command fdfdff;" (    fair quality)

What I want to do is
--Look for all instances where there is ( fair quality ) ( note that spacing between ( and fair is variable and then delete the entire statement with ( fair quality )
o/p is like this :

---- "command xyz d 
   command rereredfdfdre 
   command ffdfdfdf ;" ( good quality)
---- "command xffddyz d 
   command rerfdferere 
   command ;" ( good quality)

I tried some basic regex replace search for ( fair quality)but how do I delete the entire string before it from x to y ?
Please dont assume the spacing pattern is regular. It will change
e.g

"command    xyz..(   fair quality);"
"command  xyz (fair quality   );"

there isnt any regularity in the spacing between 2 strings , which could be of use for regex work.
Also sometimes there are needless newlines in between e.g.

"command    
xyz..(   fair 
quality);"

So end of the line assumption also does not help So the precise regex logic would be
-- Go find me instances of fair
-- Find 1st instance of command before that instance of fair
-- Find 1st instance of " before command . We will call this X
-- Find 1st instance of " after fair . We will call this Y
-- Delete from X to Y

Upvotes: 0

Views: 803

Answers (2)

Toto
Toto

Reputation: 91430

You could do:

  • Find what: -+\s*"[^"]+?"\s*\(\s*fair quality\)
  • Replace with: NOTHING
  • Repalce all

Explanation:

-+              : one or more dash
\s*             : 0 or more spaces
"               : a double quote
[^"]+?          : one or more any character that is not a double quote
"               : a double quote
\s*             : 0 or more spaces
\(              : an open parenthesis
\s*             : 0 or more spaces
fair quality    : literally 'fair quality'
\)              : a close parenthesis

Upvotes: 2

Skycc
Skycc

Reputation: 3555

you need regex with temper greedy token solution

find

-*\s*"command\b((?!\bquality\b\s*\)).)*\(\s*fair\s+quality\s*\)(;")?\r?\n?

replace with empty, make sure Regular expression and . matches newline is selected

regex details:

  • -*\s*"command\b - matches 1st instance of " before command
  • ((?!\bquality\b\s*\)).)* - match any char that is not part of quality) with temper greedy token
  • \(\s*fair\s+quality\s*\)(;")?\r?\n? - match the fair quality part, including ;" and newline behind

input test

-- "command xyz d 
   command rererere 
   command ;" ( fair quality)
---- "command xyz d 
   command rereredfdfdre 
   command ffdfdfdf ;" ( good quality)
---- "command xffddyz d 
   command rerfdferere 
   command ;" ( good quality)
----"command fdfdfxyz d 
   command refffdrerere 
   command fdfdff;" (    fair quality)
  ----"command fdfdfxyz d 
   command refffdrerere 
command    
xyz..(   fair 
quality);"
"command    xyz..(   fair quality);"
"command  xyz (fair quality   );"

output

---- "command xyz d 
   command rereredfdfdre 
   command ffdfdfdf ;" ( good quality)
---- "command xffddyz d 
   command rerfdferere 
   command ;" ( good quality)

Upvotes: 1

Related Questions