# Find and Replace using RegEx

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Why don't you write a small parser to do that for you? - Shouldn't be more than a few dozen lines.

##### 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 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.

find:
KEY_{[A-Z]*}

replace with:
cmp_key(KEY_\1);

##### Share on other sites
Quote:
 Original post by jonahrowleySorry, 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 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 on other sites
Yikes! It worked perfectly. Thank you

1. 1
2. 2
frob
13
3. 3
4. 4
5. 5

• 9
• 13
• 14
• 67
• 14
• ### Forum Statistics

• Total Topics
632132
• Total Posts
3004299

×