Mad_Mac has asked for the wisdom of the Perl Monks concerning the following question:

If I am looping over files and I want to remove any occurrences of "deez" or "nutz" in the file names, I can write a regex like this:

s/(deez|nutz)//gi;

But, if I have a file named "deez blah nutz.txt" only the deez will be replaced. And, if "deez" occurred more times in the string, all of those subsequent occurrences would also be replaced because I used the g modifier. Alternatively, if I have a file named "nuts blah deez.txt", only the nuts will be replaced. Do I have this right?

Is it possible to write a single substitution that will replace all occurrences of both words in a string? I know it can easily be done by splitting the substitution up into two separate regex's, but I'd really like to do this in a single statement, if possible.

Thanks

Replies are listed 'Best First'.
Re: Regex "or"
by moritz (Cardinal) on Mar 20, 2011 at 08:08 UTC

      That's what I thought originally, but then I wasn't getting the results I expected in my more complicated code. Which made me think I was mis-understanding the way /g works.

      Apparently, I was correct to begin with, and I have a different problem with my code :-/

Re: Regex "or"
by ikegami (Patriarch) on Mar 20, 2011 at 07:47 UTC

    It would have taken less time to try it than it did to type up the question