6

I'm looking for a desktop "Regular Expression 'Scripting' Tool" for Windows.

I have been using Replace Text but there are issues with it in the non-standard way it handles Regex strings. Here are some of them:

  1. It doesn't support "^" or "$" for anchoring your search to the beginning or end of a line, so you have to search for "newlines" surrounding a line.

  2. It uses a non-standard token to represent "newlines" (etc ...), so there is some translation required to get working Regex Search and Replace strings to work.

  3. By default, all searches are "case-sensitive". For non-regex (plain text) searches, you can specify "case-insensitive" searches, but for "Regex", all searches are "case-sensitive".

  4. The "." (dot) is handled differently than normal in that "." always matches a "newline", further complicating translation of working Regex Search and Replace strings.

Aside from these issues (and some others), it works fairly well, and it has some nice features, but it is always a problem to translate a Regex string that you have composed and tested somewhere else, to work with this tool.

To use the tool, you specify a pair of strings: a Regex search string and a Regex replacement string. You can specify as many of these pairs of strings as you want.

You also specify your input filename (or multiple files using wildcards like "*.txt") and if you want the output files to get new names or overwrite the input files (and some other options).

Then you run the tool and the set of Regex's is "applied" to the file (or each file in a set of files), and the resulting output is written to the file (files) that you specified.

So, I'm looking for a similar tool, that can process one or more files, and apply one or more Regex search/replace operations on each file.

I'm looking for a desktop tool (not online) that at least works on Windows 7, and uses at least one (more is better) standard Regex syntax, and includes the ability to do both case-sensitive and case-insensitive searches.

I'd prefer a free tool (as in free beer), but I'd be interested in hearing about outstanding purchased solutions for the future.


Edit:

Steve Barnes asked about a GUI which I should have mentioned above...

If the tool has GUI, that's fine, but I'd like to be able to use the tool to process files from the command prompt (or batch script).

Currently, using the "Replace Text" tool mentioned above, I generally use its GUI to define the fileset, path, options, etc..., and then I save the configuration to a "control" file that the tool will use when it's run to process the files. Then I manually add/edit the Regex strings in that file. For simple tasks, I just copy and edit an existing "control" file and change the path/filenames and Regex strings. Then, from the command prompt (or batch script), I run the tool and pass it command line options to tell it to use the information in the "control" file to process the files and then exit.

A recommended replacement tool does not have to work exactly this way, and doesn't need to have a GUI.

I'd like to be able to specify the Regex search/replace strings in a file, and then pass the name of that file to the tool to process the file. If I had to pass the literal Regex strings to the tool on the command line it would be an issue because it would be difficult to pass 50 or 100 or perhaps more Regex string pairs on the command line, and particularly since quoting would likely be a complication. If I had to pass the path/filenames to the tool on the command line, that would work fine.

  • 1
    Do you need a GUI or would command line meet your needs? – Steve Barnes Oct 9 '14 at 5:08
  • Is a dependency on cygwin an option? – Tim Post Oct 9 '14 at 7:18
  • @SteveBarnes - I should have mentioned it before... I edited my question to add info about the GUI. Thanks. – Kevin Fegan Oct 10 '14 at 9:14
  • @TimPost - I'd prefer a non-cygwin solution because I don't have cygwin installed and I'm not that familiar with it. But I would consider trying and using a cygwin solution. – Kevin Fegan Oct 10 '14 at 9:17
6

RegexBuddy for analyzing and tweaking regular expressions. This is one of my most important tools.

RegexMagic for hand-held creation.

There's also a big list of tools at the bottom of the Stack Overflow Regular Expressions FAQ.

RegexBuddy (this is the part I use the most by far--analysis):

enter image description here

RegexMagic:

enter image description here


Regarding scripting, I know of the RegReplace package for Sublime Text, and for single replacements, you could consider TextPad and WildEdit, which are both excellent for finding and replacing text in multiple files.

  • +1 These are some neat tools. Do you mind also expanding the answer with some feature hightlights that match the OP's concerns? – Tymric Oct 9 '14 at 14:48
  • 1
    @Timmy Added a screenshot from both. – aliteralmind Oct 10 '14 at 15:18
  • @KevinFegan: Added a couple scripting comments. – aliteralmind Oct 23 '14 at 15:27
2

I use Strawberry Perl for regex operations. It is a free and open source distribution of Perl that works on Windows, and it has a portable version. Perl regex is strong and easy to pick up, and it matches your criteria:

  1. Uses ^ and $ to indicate the beginning and the end of a line, respectively
  2. Uses the \n token for newline on Windows
  3. It is case sensitive by default, but you can add i to the end of your search sequence to make it case insensitive. E.g. /abc/i will match: Abc, ABc, abC, etc...
  4. The dot . is a wildcard, and represents any character except newline. \. represents a literal dot. You can add the /s option to include newline as well.

To perform a quick search and replace, you need to enter this syntax in command line: perl -pi.bak -e "s/PATTERN/REPLACEMENT/gi" <filename>

i.bak creates a backup and overwrites the orginal file. g is for global search, and i is for case insensitive

To specify a set of files with a wildcard, you can use the cmd for loop: for %f in (*.txt) do perl -pi.bak -e "s/PATTERN/REPLACEMENT/gi" %f

For multiple replacements and more complex operations, you can write them in a simple Perl script that takes a file name as an argument and run it using: for %f in (*.txt) do perl myscript.pl %f

It's easy to learn and you can find everything you need to start in the introduction page of the documentation


EDIT:

An example Perl script with Regex:

use strict;
use warnings;

if (@ARGV != 2){# Test for correct number of arguments
  print "This script takes exactly 2 arguments";
  exit;
}

# Open input and output files
open(my $in,  "<", $ARGV[0]) or die "Can't access input file: $!";
open(my $out, ">", $ARGV[1]) or die "Can't write output file: $!";

while (<$in>) {# Loop over the lines of the input file
  if(/text1/i) {# check if line contains "text1"
    s/text1/replaced/gi # replace all instances of "text1" with "replaced"
  }
  print $out $_ # Write the modified line into the output file
}

Run for single file using:

perl myscript.pl inputfile.txt outputfile.txt

For multiple files:

for %f in (*.txt) do perl myscript.pl %f %~nf_out.txt
  • 1
    "4. The dot . represents any character. \. represents a literal dot" For the tool I have been using, it also uses a \. to represent a literal dot which seems to be the "standard" way. The issue is that for this tool, a . (without a "\") will match any character including a newline (there is no option to change this behavior), so, specifying .+ will match the entire input file. Normally, standard Regex uses a . (without a "\") to match any character except a newline. As for "Strawberry Perl", it looks interesting and I'll take a closer look at it. – Kevin Fegan Oct 10 '14 at 9:33
  • @KevinFegan In Perl, by default the . wildcard represents any character except newline, but it can by configured to do so (I'll edit the answer to convey that) – Tymric Oct 10 '14 at 9:40
  • 1
    Just to be clear, the behavior of . with the tool I'm using now is typically undesireable. I usually have to replace all .'s with something like [^<newline>]+. – Kevin Fegan Oct 10 '14 at 9:46
  • @KevinFegan I understand, but it's nice to have options. And complying with standard Regex means more portability with less editing – Tymric Oct 10 '14 at 9:51
  • Yes, right now I don't have that option, so having the option (using Perl) would be nice because you might want either behavior in various situations. – Kevin Fegan Oct 10 '14 at 16:04
1

If you care about keeping the "feel" (compatibility) with the original grep "per-se" (short of running Cygwin), I'd recommend GNU Grep. Of course, this means:

  • no GUI
  • you'd need to use the equivalents of other Linux commands, piping them as you need (you can grab them here); for example, grep will search but won't be enough to do the replace as well.
  • 1
    Right. I use GNU Grep on Windows all the time as a Regex search replacement for the "find" command. Since Grep doesn't do "Replacements" (search only), I also use GNU Sed for a general purpose Regex search/replace tool. The problem is, neither of these tools handle multiline search/replace which is one of my requirements. – Kevin Fegan Apr 23 '18 at 15:08

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.