简体   繁体   中英

How to find and replace instances with regex

I'm trying to reformat some data that I have that isn't playing well when I copy text from a pdf.

Cordless
9B12071R
CHARGER, 3.6V,LI-ION
Cordless
9B12073R
CHARGER,NI-CD,FRAMER
Framing / Sheathing tools
F28WW
WIRE COLLATED FRAMIN
Framing / Sheathing tools
N89C-1
COIL FRAMING NAILR
Framing / Sheathing tools
N80CB-HQ

I want to have it formatted like this:

Cordless      9B12071R     CHARGER, 3.6V,LI-ION
Cordless      9B12073R     CHARGER,NI-CD,FRAMER
....

What I'm trying to do is a find and replace that replaces the first two new lines "\\n" with a tab "\\t" and leaving the third "\\n" in tact.

The first thing I do is replace all "\\n" with "\\t" which is easy. After that, I want to replace the third "\\t" with "\\n". How would I do that using regex?

For EditPadPro, paste this into the Search box

([A-Za-z /]+)
([A-Za-z0-9_-]+)
(.*)

Paste this into the Replace box

\1  \2  \3

And that should do it. Basically you can add carriage returns and tabs using Ctrl + Enter and Ctrl + Tab in EditPadPro.

I had to add a carriage return to your text in the question as it's missing the last line I think. All the others are in triples of data.

Alright here is the php code that does exactly as you want:

<?php
   $s = "Cordless
   9B12071R
   CHARGER, 3.6V,LI-ION
   Cordless
   9B12073R
   CHARGER,NI-CD,FRAMER";

   $p = '/(Cordless.*?)\\n(.+?)\\n(CHARGER.+?)(\\n|$)/s';
   $r = '\\1' . "\t" . '\\2' . "\t" . '\\3' . "\n";

   echo preg_replace($p, $r, $s);
?>

OUTPUT:

>php -q regex.php
Cordless        9B12071R        CHARGER, 3.6V,LI-ION
Cordless        9B12073R        CHARGER,NI-CD,FRAMER

Is this a regex job or can you rely on the line number?

$ perl -nE 'chomp; print $_, $.%3? "\t": "\n"' file

EDIT (after comment)

If you have to do this in an editor, then this works in vim:

%s/\(.\+\)\n\(\C[A-Z0-9-]\+\)\n\(.\+\)/\1^I\2^I\3/

The important bit here is the assumption that a line that consists entirely of AZ , 0-9 and - constitutes a part number. ^I is a tab, you type tab and vim prints ^I . (I hope your editor has this many steroids!)

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