in reply to Replacing the pattern

First, change your substitution regexp to: s/$pattern/' ' x length( $1 )/gie; ...note the use of $1 instead of $&. It is advisable to always avoid $& if possible for performance reasons.

Now to answer your question: Use a second regexp to keep it simple, and it should look like this:

$target_data =~ s/(\n{2,})/'\n' . ( ' ' x length( $1 ) ) /ge;

That converts any blank lines to a line with spaces in it equalling the number of blank lines found in a row. If you don't want the blank spaces to occupy a new line, remove the '\n' part from the right side of the substitution regexp.


Dave

Replies are listed 'Best First'.
Re^2: Replacing the pattern
by agynr (Acolyte) on Jan 20, 2005 at 10:00 UTC
    This is not what I want. I want is to change the lines if any in that pattern found, not in the whole data. The sample data is
    </TABLE> 2 <PAGE> abcdefghijklmnopq
    I hope that u understand my problem.

      davido++ has given you a solution which replaces every newline with a space. Isn't that exactly what you're asking for?

      If not, you'll need to show several examples - enough that we can see what you want. Make sure the spaces are countable.

      After Compline,
      Zaxo

        Suppose the code is given as
        </TABLE> 2 <PAGE> abcdefghijklmnopq uvw
        It should be converted to
        </TABLE> abcdefghijklmnopq uvw
        I hope that the above example will explain u everything. If any pattern (like above it is found) then it will convert the pattern length and the no. of lines in the pattern found to spaces. But if suppose no pattern is found then it shouldn't convert the lines to spaces.
        My Data is suppose this
        </TABLE> 2 <PAGE> RISK/RETURN SUMMARY AND FUND EXPENSES PRIME MONEY MARKET FUND
        It should be converted to
        </TABLE> RISK/RETURN SUMMARY AND FUND EXPENSES PRIME MONEY MARKET FUND
        Hope that it will give a better look.

        Edit by castaway - closed hanging code tag