简体   繁体   中英

Regex replace matched and remove not matched

Is there a way to replace part of matched line and remove all not matched lines with only one query using Notepad++ Replace tool?

For example: (string in quotes should be replaced with 123)
input:

There is line with "quoted" part
There is another line

expected result:

There is line with "123" part

As a pattern use

([^"]*?)^(.*?)".*?"(.*)([^"]*$)

and as a replacement use

$2"123"$3

My best shot:

Find

(^[^\r\n]*?)("quoted")([^\r\n]*?)$((\r\n)?(?![^\r\n]*?"quoted"[^\r\n]*?$).*?$)+

replace with

$1"123"$3

For every pattern between "" Ωmega answer is good, this one lets you replace a specific pattern and not everything between quotes.

Explanation:

  • (^[^\\r\\n]*?) : Find any character sequence without newlines (and non greedily)
  • ("quoted") : followed by "quoted"
  • ([^\\r\\n]*?)$ : till the end of the line
    • ((\\r\\n)? :we are in the new line
    • (?![^\\r\\n]*? : look ahead (negative) to avoid a non-newline character sequence
    • "quoted"[^\\r\\n]*?$) : followed by "quoted"
  • .*?$)+ : one or more times

Tested in np++ v6.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM