Sign in to follow this  
Schwammerl-Bob

Find and Replace using RegEx

Recommended Posts

I have this really simple task : replace KEY_LBUTTON // Left mouse button with cmp_key(KEY_LBUTTON); // Left mouse button with ~180 keys and I don´t have a clue how to do. I´ve read about using regEx with backreferences, but VC++ 2005 Express says : "Tagged expression number out of range" everytime. Thanks :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why don't you write a small parser to do that for you? - Shouldn't be more than a few dozen lines.

Share this post


Link to post
Share on other sites
I don't know about VC++ 2005's regex engine; in Perl the statement would be:

open(HANDLETOCPPFILE, "<somefile.cpp");
my @file = <HANDLETOCPPFILE>

foreach my $line (@file)
{
$line =~ s/(KEY_\w+)/cmp_key\($1\)/g;
print("$line");
}

That would read in the file, make the changes in memory and print out the new lines (which you could then easily catch by piping them into another cpp file).

Also, maybe that regex will just work in VC++ 2005. Good luck.

Share this post


Link to post
Share on other sites
Sorry, not making a suggestion, but mine would be along the lines of Muse's. I just find it amusing that Windows users have trouble with things like this. On *nix, you could a) use one of the command-line tools built for things like this b) use any one of ~5 programming languages that comes with the OS and do this in under 10 lines of code c) use one of the "big" two editors (Vim or Emacs) and have this done in a single command.

Share this post


Link to post
Share on other sites
Quote:
Original post by jonahrowley
Sorry, not making a suggestion, but mine would be along the lines of Muse's. I just find it amusing that Windows users have trouble with things like this.


They shouldn't; native Windows ports are available for both Python and Perl - you don't even need Cygwin.

Share this post


Link to post
Share on other sites
Assuming you're doing this with the find/replace dialog, you need to put {KEY_:w+} in the find what box and cmp(\1); in the replace with box.

KEY_:w+ matches anything which starts with KEY_ followed by one or more alphabetic characters. The {} braces tag it so you can refer to it in the replacement expression. As it's the first (and only) tag you refer to it as \1 in the replacement expression so putting cmp(\1); shoves inside the brackets giving you what you need.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this