正则表达式查找/替换的用例

发布于 2024-07-05 13:16:00 字数 846 浏览 4 评论 0原文

我最近和一位同事讨论了编辑。 他使用一个不太受欢迎的编辑器,而我使用另一个(我不会说是哪一个,因为它不相关,而且我想避免编辑器的激烈争论)。 我是说我不太喜欢他的编辑器,因为它不允许你用正则表达式查找/替换。

他说他从来不想这样做,这很令人惊讶,因为这是我发现自己一直在做的事情。 然而,我一时想不出超过一两个例子。 这里有人可以提供一些他们发现正则表达式查找/替换在编辑器中有用的例子吗? 从那时起,我就能够想出以下内容,作为我实际上必须做的事情的示例:

  1. 从文件中的每一行中删除行的开头,如下所示:
    第 25634 行:
    第 632157 行:

  2. 略有不同,并一次性从所有文件中删除前 19 行。

  3. 将 MySQL select 语句的结果通过管道传输到文本文件中,然后删除所有格式化垃圾并将其重新格式化为 Python 字典,以便在简单的脚本中使用。

  4. 在没有转义逗号的 CSV 文件中,将每行第 8 列的第一个字符替换为大写 A。

    在没有转义逗号的 CSV 文件中,将每行
  5. 给定一堆 GDB 堆栈跟踪,其中包含诸如
    之类的行 #3 0x080a6d61 in _mvl_set_req_done (req=0x82624a4, result=27158) at ../../mvl/src/mvl_serv.c:850
    去掉每行中除了函数名称之外的所有内容。

还有其他人有现实生活中的例子吗? 下次出现这个问题时,我希望做好更充分的准备,列出一些很好的示例来说明为什么此功能很有用。

I recently discussed editors with a co-worker. He uses one of the less popular editors and I use another (I won't say which ones since it's not relevant and I want to avoid an editor flame war). I was saying that I didn't like his editor as much because it doesn't let you do find/replace with regular expressions.

He said he's never wanted to do that, which was surprising since it's something I find myself doing all the time. However, off the top of my head I wasn't able to come up with more than one or two examples. Can anyone here offer some examples of times when they've found regex find/replace useful in their editor? Here's what I've been able to come up with since then as examples of things that I've actually had to do:

  1. Strip the beginning of a line off of every line in a file that looks like:
    Line 25634 :
    Line 632157 :

  2. Taking a few dozen files with a standard header which is slightly different for each file and stripping the first 19 lines from all of them all at once.

  3. Piping the result of a MySQL select statement into a text file, then removing all of the formatting junk and reformatting it as a Python dictionary for use in a simple script.

  4. In a CSV file with no escaped commas, replace the first character of the 8th column of each row with a capital A.

  5. Given a bunch of GDB stack traces with lines like
    #3 0x080a6d61 in _mvl_set_req_done (req=0x82624a4, result=27158) at ../../mvl/src/mvl_serv.c:850
    strip out everything from each line except the function names.

Does anyone else have any real-life examples? The next time this comes up, I'd like to be more prepared to list good examples of why this feature is useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

雾里花 2024-07-12 13:16:00

昨天,我为 Oracle 表创建了一条创建表语句,并使用 JDBC 和PreparedStatements 将字段转换为 setString() 方法调用。 表的字段名称映射到我的类属性,因此正则表达式搜索和替换是完美的选择。

创建表文本:

...
field_1 VARCHAR2(100) NULL,
field_2 VARCHAR2(10) NULL,
field_3 NUMBER(8) NULL,
field_4 VARCHAR2(100) NULL,
....

我的正则表达式搜索:

/([a-z_])+ .*?,?/

我的替换:

pstmt.setString(1, \1);

结果:

...
pstmt.setString(1, field_1);
pstmt.setString(1, field_2);
pstmt.setString(1, field_3);
pstmt.setString(1, field_4);
....

然后我手动设置每次调用的位置 int ,并在必要时将方法更改为 setInt() (和其他),但这对我来说很方便。 我实际上使用它三四次来进行类似的字段到方法调用的转换。

Yesterday I took a create table statement I made for an Oracle table and converted the fields to setString() method calls using JDBC and PreparedStatements. The table's field names were mapped to my class properties, so regex search and replace was the perfect fit.

Create Table text:

...
field_1 VARCHAR2(100) NULL,
field_2 VARCHAR2(10) NULL,
field_3 NUMBER(8) NULL,
field_4 VARCHAR2(100) NULL,
....

My Regex Search:

/([a-z_])+ .*?,?/

My Replacement:

pstmt.setString(1, \1);

The result:

...
pstmt.setString(1, field_1);
pstmt.setString(1, field_2);
pstmt.setString(1, field_3);
pstmt.setString(1, field_4);
....

I then went through and manually set the position int for each call and changed the method to setInt() (and others) where necessary, but that worked handy for me. I actually used it three or four times for similar field to method call conversions.

开始看清了 2024-07-12 13:16:00

正则表达式可以轻松地使用单词边界替换整个单词。

(\b\w+\b)

因此,您可以替换文件中不需要的单词,而不会干扰 Scunthorpe 等单词

Regex make it easy to replace whole words using word boundaries.

(\b\w+\b)

So you can replace unwanted words in your file without disturbing words like Scunthorpe

森林很绿却致人迷途 2024-07-12 13:16:00

就在上周,我使用正则表达式查找/替换将 CSV 文件转换为 XML 文件。

其实很简单,只需切碎每个字段(幸运的是它没有任何转义逗号),然后用适当的标签代替逗号将其推回原处。

Just last week, I used regex find/replace to convert a CSV file to an XML file.

Simple enough to do really, just chop up each field (luckily it didn't have any escaped commas) and push it back out with the appropriate tags in place of the commas.

热情消退 2024-07-12 13:16:00

我同意你的345点,但不一定同意12点

在某些情况下,使用匿名键盘宏更容易实现12

我的意思是执行以下操作:

  1. 将光标定位在第一行
  2. 开始键盘宏录制
  3. 修改第一行
  4. 将光标定位在下一行
  5. 停止录制。

现在修改下一行所需要做的就是重复宏。

我可以在没有正则表达式的支持的情况下生活,但不能在没有匿名键盘宏的情况下生活。

I agree with you on points 3, 4, and 5 but not necessarily points 1 and 2.

In some cases 1 and 2 are easier to achieve using a anonymous keyboard macro.

By this I mean doing the following:

  1. Position the cursor on the first line
  2. Start a keyboard macro recording
  3. Modify the first line
  4. Position the cursor on the next line
  5. Stop record.

Now all that is needed to modify the next line is to repeat the macro.

I could live with out support for regex but could not live without anonymous keyboard macros.

一江春梦 2024-07-12 13:16:00

我喜欢使用正则表达式来重新格式化项目列表,如下所示:

int item1
double item2

to

public void item1(int item1){
}
public void item2(double item2){
}

这可以节省大量时间。

I like to use regexps to reformat lists of items like this:

int item1
double item2

to

public void item1(int item1){
}
public void item2(double item2){
}

This can be a big time saver.

通知家属抬走 2024-07-12 13:16:00

当有人向我发送一列患者就诊号码列表(例如 100-200)并且我需要“0000000444”、“000000004445”格式的患者时,我总是使用它。 对我来说有奇迹!

我还用它来提取电子邮件中的电子邮件地址。 我经常群发电子邮件,所有退回的邮件都会在一封电子邮件中返回。 因此,我使用正则表达式将它们全部取出,然后将它们放入字符串 var 中以从数据库中删除。

我什至编写了一个小对话框程序来将正则表达式应用到我的剪贴板。 它抓取内容并应用正则表达式,然后将其加载回剪贴板。

I use it all the time when someone sends me a list of patient visit numbers in a column (say 100-200) and I need them in a '0000000444','000000004445' format. works wonders for me!

I also use it to pull out email addresses in an email. I send out group emails often and all the bounced returns come back in one email. So, I regex to pull them all out and then drop them into a string var to remove from the database.

I even wrote a little dialog prog to apply regex to my clipboard. It grabs the contents applies the regex and then loads it back into the clipboard.

与之呼应 2024-07-12 13:16:00

我在 Web 开发中一直使用它的一件事是剥离其 HTML 标签的一些文本。 出于安全考虑,可能需要这样做来清理用户输入,或者显示新闻文章的预览。 例如,如果您有一篇包含大量 HTML 格式标记的文章,则不能只执行 LEFT(article_text,100) + '...'(加上“阅读更多”链接)并将其呈现在页面上:通过拆分 HTML 标记来破坏页面的风险。

另外,我还必须删除数据库记录中链接到不再存在的图像的 img 标签。 我们不要忘记网络表单验证。 如果您想让用户在网络表单中输入正确的电子邮件地址(从语法上来说),那么这是彻底检查的唯一方法。

One thing I use it for in web development all the time is stripping some text of its HTML tags. This might need to be done to sanitize user input for security, or for displaying a preview of a news article. For example, if you have an article with lots of HTML tags for formatting, you can't just do LEFT(article_text,100) + '...' (plus a "read more" link) and render that on a page at the risk of breaking the page by splitting apart an HTML tag.

Also, I've had to strip img tags in database records that link to images that no longer exist. And let's not forget web form validation. If you want to make a user has entered a correct email address (syntactically speaking) into a web form this is about the only way of checking it thoroughly.

满栀 2024-07-12 13:16:00

我刚刚将一个长字符序列粘贴到一个字符串文字中,现在我想将其分解为较短字符串文字的串联,这样它就不会换行。 我还希望它可读,所以我只想在空格后中断。 我选择整个字符串(减去引号)并使用此正则表达式执行仅选择内的全部替换:

/.{20,60} /

...以及此替换:

/$0"¶         + "/

...其中 pilcrow 是实际的换行符,并且空格数各不相同从一个事件到下一个事件。 结果:

String s = "I recently discussed editors with a co-worker. He uses one "
         + "of the less popular editors and I use another (I won't say "
         + "which ones since it's not relevant and I want to avoid an "
         + "editor flame war). I was saying that I didn't like his "
         + "editor as much because it doesn't let you do find/replace "
         + "with regular expressions.";

I've just pasted a long character sequence into a string literal, and now I want to break it up into a concatenation of shorter string literals so it doesn't wrap. I also want it to be readable, so I want to break only after spaces. I select the whole string (minus the quotation marks) and do an in-selection-only replace-all with this regex:

/.{20,60} /

...and this replacement:

/$0"¶         + "/

...where the pilcrow is an actual newline, and the number of spaces varies from one incident to the next. Result:

String s = "I recently discussed editors with a co-worker. He uses one "
         + "of the less popular editors and I use another (I won't say "
         + "which ones since it's not relevant and I want to avoid an "
         + "editor flame war). I was saying that I didn't like his "
         + "editor as much because it doesn't let you do find/replace "
         + "with regular expressions.";
不弃不离 2024-07-12 13:16:00

我对任何编辑器所做的第一件事就是尝试找出正则表达式的奇怪之处。 我用它所有的时间。 没什么真正疯狂的,但是当您必须在不同类型的文本之间复制/粘贴内容时,它很方便 - SQL <-> PHP 是我最常使用的一种 - 并且您不想浪费时间进行 500 次相同的更改。

The first thing I do with any editor is try to figure out it's Regex oddities. I use it all the time. Nothing really crazy, but it's handy when you've got to copy/paste stuff between different types of text - SQL <-> PHP is the one I do most often - and you don't want to fart around making the same change 500 times.

百变从容 2024-07-12 13:16:00

每当我尝试替换跨多行的值时,正则表达式都非常方便。 或者当我想用包含换行符的值替换值时。

我还喜欢您可以匹配正则表达式中的内容,而不是使用 $# 语法替换完整匹配来输出您想要维护的匹配部分。

Regex is very handy any time I am trying to replace a value that spans multiple lines. Or when I want to replace a value with something that contains a line break.

I also like that you can match things in a regular expression and not replace the full match using the $# syntax to output the portion of the match you want to maintain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文