Can someone, please, help me to construct the regexp to remove the matched pattern from the string, but to avoid more than one space in the resulting string at the boundaries of the removed substring. All of the remaining substring should stay untouched.

Note: The blue text is a condition I forgot to insert in the inital text.

Example:

Code

Input:

one two some_pattern three four one two some_pattern three four one twosome_pattern three four one two some_patternthree four one two three four some_pattern some_pattern one two three four onetwosome_patternthreefour one two some_pattern three four

Desired output:

one two three four one two three four one two three four one two three four one two three four one two three four onetwothreefour one two three four

Yes, that could be a soution. However, I wouldn't want to 'touch' the unmatched part of the string. I am sorry my explanation wasn't precise in that detail. Here you are an additional example to demonstrate the desired output:

Code

Input:

one two some_pattern three four

Desired output:

one two three four

That is - all of the remaining (unmatched) substring should stay as it was.

I am sure that Laurent is on the right track. This will take more than one Regex. I recommend that you implement the solution as a subroutine. For testing purposes, it may worth the extra effort to package that subroutine as a module.

EDITS: I have replaced the code in this post. The failures reported by the original were correct. I have improved the output of the test and replaced the regular expressions with a new set. The new subroutine now passes all but the blue test case. The test case appears to have too many spaces between 'one' and 'two'. I have taken the liberty of changing that in my test. The code now posted below passes all tests (including the extra one I describe below)

Code

use strict; use warnings; use Test::More qw( no_plan );

my %fixed_line = ( 'one two some_pattern three four' => 'one two three four', 'one two some_pattern three four' => 'one two three four', 'one twosome_pattern three four' => 'one two three four', 'one two some_patternthree four' => 'one two three four', 'one two three four some_pattern' => 'one two three four', "one two three four some_pattern\n" => "one two three four\n", 'some_pattern one two three four' => 'one two three four', 'onetwosome_patternthreefour' => 'onetwothreefour', 'one two some_pattern three four' => 'one two three four', );

There are other issues which should be tested. Regexps treat Tabs and Newlines as whitespace. That may not be what you want. (e.g. your fifth case would be much different there were a newline at the end.)

I suspect that you will continue to discover special cases for some time. This kind of test will assure you that proposed fixes do not break the old code. Good Luck, Bill