简体   繁体   中英

Regex find string and replace that line and following lines

I am trying to find a regex to achieve the following criteria which I need to use in javascript.

Input file

some string is here and above this line
:62M:C111111EUR1211498,00
:20:0000/11111000000
:25:1111111111
:28C:00001/00002
:60M:C170926EUR1211498,06
:61:1710050926C167,XXNCHKXXXXX 11111//111111/111111

Output has to be

some string is here and above this line
:61:1710050926C167,XXNCHKXXXXX 11111//111111/111111

Briefly, find :62M: and then replace (and delete) the lines starting with :62M: followed by lines starting with :20:, :25:, :28c: and :60M:.

Or, find :62M: and replace (and delete) until the line starting with :61:.

Each line has fixed length of 80 characters followed by newline (CR LF).

Is this really possible with regex? I know how to find a string and replace the same line where the string is. But here multiple lines to be removed which is quite hard for me.

Please could someone help me out if it is possible with regex.

Here it is. First I'm finding text to delete using regex (note that I'm using [^]* to match all the lines insted of .* , as it also matches newlines). Then I'm replacing it with a newline.

 var regex = /:62M:.*([^]*):61:.*/; var text = `some string is here and above this line :62M:C111111EUR1211498,00 :20:0000/11111000000 :25:1111111111 :28C:00001/00002 :60M:C170926EUR1211498,06 :61:1710050926C167,XXNCHKXXXXX 11111//111111/111111`; var textToDelete = regex.exec(text)[1]; var result = text.replace(textToDelete, '\\n'); console.log(result);

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