tags:

views:

589

answers:

8

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

A: 

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

Usually with the ! operator.

Alan
+1  A: 

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/

Bart
A: 

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?

Captain Segfault
Nothing at all (as I mention above, I use it regularly, probably daily).
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>
Eddie
I'm spending most of my time working in a gui editor right now so it's mostly a work-flow thing. I use grep frequently, but mostly at work where I do systems work and spend most of my time in ssh sessions, there vi/grep/awk/sed are perfect, I just thought maybe I was missing something.
I don't think you need "cat filename | " in the second example. Doesn't grep also work like "grep expression filename"?
gpojd
+3  A: 

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/'
Brian Carper
A: 

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.

A: 

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.

gpojd
A: 

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

Tinidian