#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1

    Using variables within a REGEX search pattern


    Is there any way one can do that.

    For example:

    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    
    my $strand = "tttgctttttgctttttcgttt";
    my $length = 3;
    while ($strand =~ /(t{$length,})/g){
    print $1,"\n";
    Even when I escape the characters, this code still is nonfunctional.

    Since this is code is part of a function that is dependent on user input, I need to be able to search for a variable.

    Is there anyway REGEX can do that??

    Edit: let me tell you what this code does. It it looking for all occurences within the string where t appears 3 or more times. The poly_t sequence.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    It's failing because you left out the closing brace on the while loop.

    This works:
    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    
    my $strand = "tttgctttttgctttttcgttt";
    my $length = 3;
    while ($strand =~ /(t{$length,})/g){
        print $1,"\n";
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1
    Originally Posted by FishMonger
    It's failing because you left out the closing brace on the while loop.

    This works:
    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    
    my $strand = "tttgctttttgctttttcgttt";
    my $length = 3;
    while ($strand =~ /(t{$length,})/g){
        print $1,"\n";
    }
    I didn't forget to put it, I just missed it when I was doing the paste job. My code is exactly as you have typed it, it still doesn't work.

    But I COULD incorporate variables into REGEX than?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    37
    Rep Power
    1
    Originally Posted by FishMonger
    It's failing because you left out the closing brace on the while loop.

    This works:
    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    
    my $strand = "tttgctttttgctttttcgttt";
    my $length = 3;
    while ($strand =~ /(t{$length,})/g){
        print $1,"\n";
    }
    Never mind I figured out my mistake, LMAO A friggin comma just thrown on the floor. I hate those kinda mistakes.

    Thanks for the help Fish.

IMN logo majestic logo threadwatch logo seochat tools logo