25

I have a file with multiple instances of Text_1 and Text1 and I need to replace both those strings with Text_A and TextB respectively.

Currently I'm doing two Find and Replace functions on each file one that finds Text_1 and replaces it with Text_A and the other that finds Text1 and replaces it with TextB.

Is there any way to do this all at once instead of having to run "Find and Replace" twice?

I am using Dreamweaver CS3, but I also have Notepad++, regular Notepad, OO Writer, MS Word if those will be easier. Ideally I could do this in Dreamweaver or Notepad++ but I'm open to downloading something else to get the job done. I'd prefer not to have to do any command line stuff or create a batch file (while I'm aware of it, I don't understand it really).

In case the above description isn't clear, let me explain it this way...

I want to run Find & Replace 1 time in 1 document and I want it to do ALL of the following during that one Find & Replace instance:

  1. Find Text_1 and Replace with Text_A
  2. Find Text1 and Replace with TextB

I am not trying to do a Find and Replace across several documents.

1
  • It might be possible using Regexp find and replace if there's a distinct pattern. Else it's not possible @matt
    – Sathyajith Bhat
    Nov 15, 2010 at 15:16

8 Answers 8

9

Take a look at Sed. You can easily achieve your goal by only one command line

sed -e "s/Text_1/TextA/" -e "s/Text1/TextB/" <your_file.txt>your_file_new.txt

38

Since Notepad++ 6.0 it is actually possible without scripts using the following technique:

Find: Text_1 and Replace with: Text_A

Find: Text2 and Replace with: TextB

Find: (Text_1)|(Text2)

Replace: (?1Text_A)(?2TextB)

The general syntax is:

Find: (FindA)|(FindB)|(FindC)...

Replace: (?1ReplaceA)(?2ReplaceB)(?3ReplaceC)...

?1 refers to the first captured phrase and so on.

I got this from the following answer: https://stackoverflow.com/a/16104946/287101

3
  • What if we need to replace with only numbers? For example, replacing month names with numbers. Sep 22, 2021 at 9:35
  • this does not work in regex101.com, any suggestions on how to adapt?
    – Stefan
    May 6, 2022 at 16:27
  • @mythofechelon enclose the replacement values in (unescaped) parenthesis like (?1(Replace#))
    – MBraedley
    May 30, 2022 at 14:52
3

Using Notepad++, You can either

  • Open all files containing the words you want to replace and make use of Find / Replace in all open files

    enter image description here

  • Use Find / Replace in files

    enter image description here

  • Record a macro performing the find and replace options and play it back

2
  • 1
    That's not what I'm trying to do. I don't want to replace it on several files at once, I want to replace several strings in one file all at once. I want to be able to run one find and replace and have it search the document and replace all instances of Text_1 with Text_A and all instances of Text1 with TextB. Two seperate strings replaced with two separate values in one file.
    – matt
    Nov 15, 2010 at 14:52
  • The image-links are broken, any chance you can fix them somehow?
    – Bobby
    Jun 10, 2013 at 11:15
2

You're better off running Find & Replace twice, because you have two different replacements happening. However, there should be very little tradeoff in processing time. Consider the following:

Running Find & Replace once, searching term by term:

Get Term
  Does term match?  If so, replace term
Get Next Term
...

For running purposes, we'll assume it's linear and say that this runs in O(n) time.

You want to find two different terms. Running Find & Replace twice looks like:

Get Term
  Does term match?  If so, replace term
Get Next Term
...

Get Term
  Does term match?  If so, replace term
Get Next Term
...

which would take O(2x1n) time, or twice as long as searching for one term.

Searching for two terms with two replacements would theoretically look like:

Get Term
  Does first term match?  If so, replace first term
  Does second term match?  If so, replace second term
Get Next Term
...

Getting the terms does not take that much processing power, so essentially you are looking up two sets of searches for each term, giving a time of O(1x2n), running once but searching two terms.

While you would save some time not loading each term twice, you would spend more time searching each term twice, so there's little tradeoff, and since this feature would be used less often than searching one term with one replacement, application developers just assume not write that functionality.

DISCLAIMER TO PROGRAMMERS: This is a generalized example. I know it is better but I'm trying to show why that feature isn't found.

1

I use TextCrawler for batch find and replace. (I use Notepad++ for single search/replace across multiple files since it is faster than Textcrawler.) TextCrawler supports regular expressions as well as normal search/find. The batch files can be saved and reloaded for future use.

3
  • Please see How do I recommend software in my answers? In particular, at a bare minimum, say where to get the software. Nov 6, 2014 at 21:07
  • The batch function is only available in the non-free Pro version of TextCrawler.
    – Hobbes
    Mar 31, 2015 at 8:42
  • TextCrawler from DigitalVolcano can do this. I recently found out EmEditor also has this feature (make sure to use Batch, NOT Bulk find/replace!) But again, you need the "pro" version. Emeditor Pro has a 30 day full trial though. You can use regex101.com to test each find/replace combo and then plug it into respective engine; EmEditor uses Boost engine (PCRE2), TextCrawler uses Java8 engine.
    – Jon Grah
    May 29, 2023 at 8:48
0

The regular expression support in Notepad++ is severely limited. Without the alternation operator, there's no hope of even coming close to what you want to do. You will need to use sed or an editor that has better regex support.

Commonly used GNU extensions to the regular expression syntax include:

  • the ? operator, meaning zero or one of a kind;
  • the | alternation operator ("foo|bar" matches either of "foo" or "bar")
  • modifiers like /g for greedy or /i for case insensitive;
  • {a,b} to denote betwen a and b tokens of a kind.
  • (pattern)+ to mach one or more matches of the same pattern

The Scitilla component, which underpins Notepad++, is strictly POSIX-compatible and does not recognise these extensions. You can consult Regular Expressions for a complete rundown and some examples

Source

See also

GNUWin32 sed for Windows

0

Also it can be done by using ActiveX Plugin (for Notepad++) and executing custom script:

NppApplication.activeEditor.text
=NppApplication.activeEditor.text
.replace(/Text_1/g,"Text_A")
.replace(/Text1/g,"TextB")
1
  • This works, but note that due to a current bug in the ActiveX Plugin a NULL character is appended to the file each time you run the script.
    – Aidan
    Oct 22, 2018 at 8:43
-1

This works for all numbers (to remove all numbers from text/article)

  1. Press Ctrl+H to open the Find and Replace dialog.

  2. In the "Find what" box, type ^#

  3. Leave the "Replace with" box empty.

  4. Replace All.

1
  • 1
    The OP doesn't just want the numbers removed, they wanted to replace it with a letter.
    – booyaa
    Jun 10, 2013 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .