文本编辑器中的正则表达式提取

发布于 2024-07-15 11:36:13 字数 1210 浏览 1 评论 0原文

我对编程有点陌生,所以如果这是非常明显的,请原谅我(这将是一个受欢迎的消息)。

我在空闲时间使用 pregmatch 进行了大量的 PHP 开发,并使用免费(开源?)Regex Tester 编写了大部分表达式。

然而,我经常发现自己想要简单地快速提取某些内容,而我知道的唯一方法就是写下我的表达式,然后编写脚本,这可能很可笑,但欢迎来到我的现实。 :-)

我想要的是一个简单的文本编辑器,我可以将表达式输入到其中(给定一个文件或充满粘贴文本的缓冲区),并让它解析表达式并返回仅包含结果的文档。

我发现的通常是正则表达式搜索/替换函数,就像在 Notepad++ 中一样,我可以轻松地使用表达式查找(并替换)所有实例,但我根本不知道如何只提取它......

而且它可能非常明显,可以表达式仅匹配反函数? 然后我可以使用类似的东西(只是我当前正在处理的表达式):

<a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>

并将所有不匹配的内容替换为任何内容。 但我确信这是常见且简单的事情,我真的很感激任何批评者。

FWIW我知道 grep 并且我可以使用它来做到这一点,但我希望他们是更好的图形化解决方案,我只是不知道。

谢谢。

Zach


我所希望的是能够在一组更标准的 GUI 工具中工作的东西(即我可能已经在使用的工具)。 我感谢所有的回复,但我希望避免使用 perl 或 vi 或 grep,否则我会自己编写脚本(当然我这样做了),因为它们都是相对强大的低级工具。

也许是我说得不够清楚。 作为一名高级系统管理员,cli 工具对我来说很熟悉,我非常喜欢它们。 在家工作,但我发现大部分时间都花在 GUI 上,例如 Netbeans 或 Notepad++。 我只是认为有一种简单的方法可以使用这些工具实现基于正则表达式的数据提取(因为在这些情况下我已经在使用它们了)。

类似于我所指的内容是 this ,它将在第一行和第二行的 url,然后提取(返回)数据。

它很丑(今晚之后我会把它拿下来,因为它可能充满了问题)。

无论如何,感谢您的回复。 我很感激。

I'm kind of new to programming, so forgive me if this is terribly obvious (which would be welcome news).

I do a fair amount of PHP development in my free time using pregmatch and writing most of my expressions using the free (open source?) Regex Tester.

However frequently I find myself wanting to simply quickly extract something and the only way I know to do it is to write my expression and then script it, which is probably laughable, but welcome to my reality. :-)

What I'd like is something like a simple text editor that I can feed my expression to (given a file or a buffer full of pasted text) and have it parse the expression and return a document with only the results.

What I find is usually regex search/replace functions, as in Notepad++ I can easily find (and replace) all instances using an expression, but I simply don't know how to only extract it...

And it's probably terribly obvious, can expression match only the inverse? Then I could use something like (just the expression I'm currently working on):

<a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>

And replace everything that doesn't match with nothing. But I'm sure this is something common and simple, I'd really appreciate any poniters.

FWIW I know grep and I could do it using that, but I'm hoping their are better gui'ified solution I'm simply ignorant of.

Thanks.

Zach


What I was hoping for would be something that worked in a more standard set of gui tools (ie, the tools I might already be using). I appreciate all the responses, but using perl or vi or grep is what I was hoping to avoid, otherwise I would have just scripted it myself (of course I did) since their all relatively powerful, low-level tools.

Maybe I wasn't clear enough. As a senior systems administrator the cli tools are familiar to me, I'm quite fond of them. Working at home however I find most of my time is spent in a gui, like Netbeans or Notepad++. I just figure there would be a simple way to achieve the regex based data extraction using those tools (since in these cases I'd already be using them).

Something vaguely like what I was referring to would be this which will take aa expression on the first line and a url on the second line and then extract (return) the data.

It's ugly (I'll take it down after tonight since it's probably riddled with problems).

Anyway, thanks for your responses. I appreciate it.

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

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

发布评论

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

评论(7

那些过往 2024-07-22 11:36:13

如果您想要一个具有良好正则表达式支持的文本编辑器,我强烈推荐 Vim。 Vim 的正则表达式引擎相当强大并且很好地集成到编辑器中。 例如,

:g!/regex/d

这表示删除缓冲区中与模式regex不匹配的每一行。

:g/regex/s/another_regex/replacement/g

这表示在与 regex 匹配的每一行上,进行另一次搜索/替换,以将与 another_regex 匹配的文本替换为 replacement

如果您想使用命令行 grep 或 Perl/Ruby/Python/PHP 任何其他工具,您可以通过该工具过滤当前缓冲区的文本并更新缓冲区以反映结果:

:%!grep regex
:%!perl -nle 'print if /regex/'

If you want a text editor with good regex support, I highly recommend Vim. Vim's regex engine is quite powerful and is well-integrated into the editor. e.g.

:g!/regex/d

This says to delete every line in your buffer which doesn't match pattern regex.

:g/regex/s/another_regex/replacement/g

This says on every line that matches regex, do another search/replace to replace text matching another_regex with replacement.

If you want to use commandline grep or a Perl/Ruby/Python/PHP one-liner any other tool, you can filter the current buffer's text through that tool and update the buffer to reflect the results:

:%!grep regex
:%!perl -nle 'print if /regex/'
凉宸 2024-07-22 11:36:13

您尝试过 nregex.com 吗?

http://www.nregex.com/nregex/default.aspx

有一个插件对于 Netbeans,但开发看起来陷入停滞:

http://wiki.netbeans.org/Regex

http://wiki.netbeans.org/RegularExpressionsModuleProposal

您也可以尝试调节器:

http://sourceforge.net/projects/regulator/

Have you tried nregex.com ?

http://www.nregex.com/nregex/default.aspx

There's a plugin for Netbeans here, but development looks stalled:

http://wiki.netbeans.org/Regex

http://wiki.netbeans.org/RegularExpressionsModuleProposal

You might also try The Regulator:

http://sourceforge.net/projects/regulator/

哆兒滾 2024-07-22 11:36:13

大多数正则表达式引擎将允许您匹配正则表达式的相反内容。

通常与 ! 操作员。

Most regex engines will allow you to match the opposite of the regex.

Usually with the ! operator.

水溶 2024-07-22 11:36:13

我知道 grep 已被提及,并且您不需要 cli 工具,但我认为 ack 值得提及。

ack是一个类似grep的工具,旨在
拥有大树的程序员
异构源代码。

ack 纯粹是用 Perl 编写的,并且
利用 Perl 的强大功能
正则表达式。

I know grep has been mentioned, and you don't want a cli tool, but I think ack deserves to be mentioned.

ack is a tool like grep, aimed at
programmers with large trees of
heterogeneous source code.

ack is written purely in Perl, and
takes advantage of the power of Perl's
regular expressions.

淡墨 2024-07-22 11:36:13

一个好的文本编辑器可用于执行您所描述的操作。 我使用 EditPadPro 进行搜索和替换功能,它还有一些其他不错的功能,包括大多数主要格式的代码着色。 搜索面板功能包括正则表达式模式,允许您输入正则表达式,然后搜索第一个实例,该实例标识您的表达式是否与适当的信息匹配,然后为您提供迭代替换或所有实例的选项。

http://www.editpadpro.com

A good text editor can be used to perform the actions you are describing. I use EditPadPro for search and replace functionality and it has some other nice feaures including code coloring for most major formats. The search panel functionality includes a regular expression mode that allows you to input a regex then search for the first instance which identifies if your expression matches the appropriate information then gives you the option to replace either iteratively or all instances.

http://www.editpadpro.com

洋洋洒洒 2024-07-22 11:36:13

我的建议是 grepcygwin 如果你被困在 Windows 盒子上。

echo "text" | grep <a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>

或者

cat filename | grep <a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>

My suggestion is grep, and cygwin if you're stuck on a Windows box.

echo "text" | grep <a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>

OR

cat filename | grep <a href="/browse/0/b/-dbm/a/0-0/1200000([^/]*)/0.html">([^<]*)</a>
不语却知心 2024-07-22 11:36:13

我想要的是类似的东西
我可以提供简单的文本编辑器
表达式到(给定一个文件或
缓冲区充满了粘贴的文本)并且有
它解析表达式并返回一个
仅包含结果的文档。

您刚刚描述了 grep。 这正是 grep 所做的。 它出什么问题了?

What I'd like is something like a
simple text editor that I can feed my
expression to (given a file or a
buffer full of pasted text) and have
it parse the expression and return a
document with only the results.

You have just described grep. This is exactly what grep does. What's wrong with it?

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