简体   繁体   English

如何使用正则表达式查找和替换实例

[英]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. 我正在尝试重新格式化一些我拥有的数据,这些数据在从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. 我正在尝试执行的查找和替换操作,将前两个新行“ \\ n”替换为选项卡“ \\ t”,而将第三个“ \\ n”保持原样。

The first thing I do is replace all "\\n" with "\\t" which is easy. 我要做的第一件事是用“ \\ t”替换所有“ \\ n”,这很容易。 After that, I want to replace the third "\\t" with "\\n". 之后,我想用“ \\ n”替换第三个“ \\ t”。 How would I do that using regex? 我将如何使用正则表达式呢?

For EditPadPro, paste this into the Search box 对于EditPadPro,请将其粘贴到“ Search框中

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

Paste this into the Replace box 将此粘贴到“ Replace框中

\1  \2  \3

And that should do it. 那应该做到的。 Basically you can add carriage returns and tabs using Ctrl + Enter and Ctrl + Tab in EditPadPro. 基本上,您可以在EditPadPro中使用Ctrl + EnterCtrl + Tab添加回车符和制表符。

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代码:

<?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: 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: 如果必须在编辑器中执行此操作,则可以在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. 此处重要的一点是假设完全由AZ0-9-组成的线构成零件号。 ^I is a tab, you type tab and vim prints ^I . ^I是一个标签,您键入tab,然后vim打印^I (I hope your editor has this many steroids!) (希望您的编辑者有这么多的类固醇!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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