RegEx string to find two strings and delete the rest of the text in the file

时光总嘲笑我的痴心妄想 提交于 2020-06-23 11:23:46

问题


I need to do a find and delete the rest in a text file with notepad+++ i want tu use RegeX to find variations on thban..... the variable always has max 5 chars behind it(see dots). with my search string it hit the last line but the whole line. I just want the word preserved. When this works i also want keep the words containing C3..... The rest of a tekst file can be delete. It should also be caps insensitive

(?!thban\w+).*\r?\n?

\

THBANES900 and C3950 bla bla
THBAN
..THBANES901.. C3850 bla bla
THBANMP900
**..thbanes900..**

This should result in

THBANES900 C3950
THBAN
THBANES901 C3850
THBANMP900
thbanes900

回答1:


Maybe just capture those words of interest instead of replacing everything else? In Notepad++ search for pattern:

^.*\b(thban\S{0,5})(?:.*(\sC3\w+))?.*$|.+

See the Online Demo

  • ^ - Start string ancor.
  • .*\b - Any character other than newline zero or more times upto a word-boundary.
  • (- Open 1st capture group.
    • thban\S{0,5} - Match "thban" and zero or 5 non-whitespace chars.
    • ) - Close 1st capture group.
  • (?: - Open non-capturing group.
    • .* - Any character other than newline zero or more times.
    • ( - Open 2nd capture group.
      • \sC3\w+ - A whitespace character, match "C3" and one ore more word characters.
      • ) - Close 2nd capture group.
    • )? - Close non-capturing group and make it optional.
  • .* - Any character other than newline zero or more times.
  • $ - End string ancor.
  • | - Alternation (OR).
  • .+ - Any character other than newline once or more.

Replace with:

$1$2

After this, you may end up with empty line you can switly remove using the build-in option. I'm unaware of the english terms so I made a GIF to show you where to find these buttons:

I'm not sure what the english checkbutton is for ignore case. But make sure that is not ticked.




回答2:


You may use

Find What:      (?|\b(thban\S{0,5})|\s(C3\w+))|(?s:.)
Replace With: (?1$1\n:)

Screenshot & settings

Details

  • (?| - start of a branch reset group:
    • \b(thban\S{0,5}) - Group 1: a word boundary, then thban and any 0 to 5 non-whitespace chars
    • | - or
    • \s(C3\w+) - a whitespace char, and then Group 1: C3 and one or more word chars
  • ) - end of the branch reset group
  • | - or
  • (?s:.) - any one char (including line break chars)

The replacement is

  • (?1 - if Group 1 matched,
    • $1\n - Group 1 value with a newline
    • : - else, replace with empty string
  • ) - end of the conditional replacement pattern


来源:https://stackoverflow.com/questions/62451653/regex-string-to-find-two-strings-and-delete-the-rest-of-the-text-in-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!