in reply to Re: Regex - remove characters (pattern not terminated)
in thread Regex - remove characters (pattern not terminated)

Forgive me for not being as clear as I thought I was.
What I have working now (as seen in the code in my last post) is a pattern that is removed (in this case it's replaced by an asterisk for testing purposes) in $temp_seq and I would like to have the preceding or following 6 characters removed as well.

For example:
$temp = 1234567890; $pattern = 123; $temp =~ s/$pattern/*/i; print "temp = $temp";
The output I would like to see is: temp = 0

Like wise, if the pattern removed was at the end of the string, I would like to remove the preceding 6 characters of the $temp.
As for the inversion of the pattern, consider the example mentioned earlier, where the pattern removed was found at the end of the string but it was inverted:
$temp = 4567890321; $pattern = 123;
Instead of creating a whole new variable to match the 321, is there a simpler way to reverse the $pattern variable explicitly in a regex?

please, let me know if I need to re-clarify anything and thanks again!!

Replies are listed 'Best First'.
Re^3: Regex - remove characters (pattern not terminated)
by almut (Canon) on Jun 28, 2010 at 20:39 UTC
    The output I would like to see is: temp = 0

    As whatever matches will be replaced, just include those 6 characters in the search pattern:

    $temp =~ s/$pattern\w{6}//i;

    As for the inversion, see reverse.

Re^3: Regex - remove characters (pattern not terminated)
by JavaFan (Canon) on Jun 28, 2010 at 21:02 UTC
    I would probably write that as:
    my $pattern = '...'; $temp =~ s/${pattern}.{6}//s || $temp =~ s/.{6}${pattern}//s;
    Not sure you need the /i, as your example uses numbers.
    Instead of creating a whole new variable to match the 321, is there a simpler way to reverse the $pattern variable explicitly in a regex?
    But what if the pattern is more complicated? And why do you consider creating a "whole new variable" such a big deal? I guess if your pattern is just a simple string without characters special to the regexp engine, you could write
    $temp =~ s/${\scalar reverse $pattern}.{6}//s || $temp =~ s/.{6}${\scalar reverse $pattern}//s;
    but I wouldn't. The following is, IMO, much simpler:
    my $rpattern = reverse $pattern; $temp =~ s/$rpattern.{6}//s || $temp =~ s/.{6}$rpattern//s;
      Thanks for the help but unfortunately I received the following error when implementing: $temp =~ s/${pattern}.{6}//s || $temp =~ s/.{6}${pattern}//s; The error message that I received was: "Use of uninitialized value in concatenation (.) or string".

      In regards to the reversing the pattern, the program that I am writing is going already VERY tedious since the data are so numerous and I'm trying to cut down on as much unnecessary memory usage as possible. It would be simpler to incorporate the 6 preceding/following characters into the pattern but unfortunately they are random sequences of "ACTG" 6 characters long. The possible situations that I would encounter while using a regex would be:
      1. Pattern found at the beginning of string
      2. Inverted pattern found at the beginning of string
      3. Pattern found at end of string
      4. Inverted Pattern found at the end of the string

      All of which would need the adjacent 6 characters removed as well as the pattern. Let me know if you can help me out! Thanks again!!