I want to use an editor to specify and delete a specific string to a specific string in the regular expression.

Asked 1 years ago, Updated 1 years ago, 322 views

I would like to delete the text from D-E-F to G-H-I from the text "before processing" below.

A-B-C;hogehogehoge
D-E-F;fugafuga
      piyopiyopiyo
G-H-I; hogerahogera

A-B-C;HOGEHOGEHOGE
D-E-F;FUGAFUGAFUGA
      PIYOPIYOPIYO
G-H-I;HOGERAHOGERA
・
・
・
A-B-C;hogehogehoge
G-H-I; hogerahogera

A-B-C;HOGEHOGEHOGE
G-H-I;HOGERAHOGERA
・
・
・

Referring to Yahoo! Chiebukuro post, I grouped A-B-C and G-H-I and wrote "Not Found" as follows:

Search string: (A-B-C)[^(G-H-I)]*(G-H-I)
Replacement string:\r\nG-H-I

It works if the search is for a single character, but isn't it enough to simply group strings?

In this case, please let me know how it is appropriate to describe it.

My software is the Merry text editor.

regular-expression editor

2023-02-02 05:59

1 Answers

For your information, I have never used the "Merry Text Editor", but here is an example of Python description (pre-processed text: temp.txt).
Use *? instead of * (for example, +???).Also, you must use [\s\S] (all characters except blank or blank) instead of . because the part you want to delete.

import re

with open('temp.txt', 'r') as f:
    before=f.read()

after=re.sub(r'(A-B-C.*\n)[\s\S]*?(G-H-I)', r'\1\2', before)
print(after, end=')
A-B-C;hogehogehoge
G-H-I; hogerahogera

A-B-C;HOGEHOGEHOGE
G-H-I;HOGERAHOGERA


2023-02-02 07:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.