2

Is there a good and cost-free tool for extracting text, using Regular Expressions? I'm using OS X.

I know that there are online tools for "testing" Regex patterns – like Regex101 – but I'm looking for a tool that can actually help me extract multi-line text using a pattern.

I have a few thousand lines of text from which I need to extract snippets from every line.

If there are multiple alternatives, I would like to know if there's any one in particular that I should know about.

  • 2
    Why a web-based tool? Wouldn't a local tool be faster for a few thousand lines? We're not recommending websites, so answers that you'll get are web-based tools that you install on your own server and then use the web-based tool. – Thomas Weller Dec 21 '15 at 20:55
  • @ThomasWeller Just because it's more practical if there's something I can use in a web browser, without having to install anything. But if you know anything for OS X, I'm interested. – Winterflags Dec 21 '15 at 20:56
  • What's your budget requirement? – Thomas Weller Dec 21 '15 at 20:58
  • Okay, I will update my question. I'm looking for a free solution. – Winterflags Dec 21 '15 at 21:00
  • Free as in "free beer" (no money) – or as in "free speech" (open source)? – Izzy Dec 21 '15 at 21:26
5

You Already Have

OS-X comes with python installed, if not it is a free download, and python comes with the re library installed by default, which is one of the best regular expression libraries that I have found, what I especially like is that you can put comments into your regular expressions.

OS-X also comes with grep & sed installed which can both operate with regular expressions.

You Can Install

If you really must work in the browser then ipython & Jupyter notebooks is a way to go not sure if it is installed by default but it is easy to install.

On the Web

You can even try ipython/Jupyter out in your browser here without installing anything.

Freedom

All of the above are free both gratis and Open Source with the possible exception of the OS/X implementations of grep & sed.

Other Platforms

grep and sed are available on most *nix platforms, i.e. OS/X, Linux, etc., but not by default on Windows, there are free packages available that do add them though, e.g. msys.

Python/iPython/Jupyter are available for just about every platform from RaspberryPy to Supercomputers and just about everything in between including OS/X, Linux & Windows.

2

You already have… grep and sed

If you’re okay with POSIX regular expressions, every unixoid system comes with grep and sed.

Extract matching lines:

grep 'pattern' file

Extract matches:

sed -n '/^.*\(pattern\).*$/s//\1/p' file

This gets a little more complicated, so create a script for it:

#!/bin/sh
pattern=$1
shift
sed -n '/^.*\('"$pattern"'\).*$/s//\1/p' "$@"

You already have… Perl

Most regular expressions examples use perlre or PCRE, which are somewhat compatible, instead (if you use things like \s, this is true for you). The Perl Regular Expressions Tutorial already contains a “simple_grep” which can do the extraction à la grep, although you can do it inline too:

perl -ne 'print if /pattern/;' file

Or just the matches (explicit newline there):

perl -ne 'print $&.$/ if /pattern/;' file

You could also do multi-line matches instead of pulling out lines, or extract multiple matches from each line; this depends on your requirements, which aren’t clear enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.