I am using the following string:
#abc#bcd#efg#hij#klm#nop
I need to write a regular expression which will find the 3rd hash and extract the entire line, including the hash to give me the following output:
#efg#hij#klm#nop
Printable View
I am using the following string:
#abc#bcd#efg#hij#klm#nop
I need to write a regular expression which will find the 3rd hash and extract the entire line, including the hash to give me the following output:
#efg#hij#klm#nop
Wow... well, my code is going to be rusty, to check the monastary.
You may be able to use # instead of \#. This is just one solution. There is also the backreferences. In any case, I strongly suggest you get the Camel book if no other. It is very useful.Code:$string =~ s/^\#.*\#.*\#/\#/;
Curses... it is up to 3rd Ed. Perhaps I should... uhm... loose mine so I can buy a new one. Someone still has my Cookbook. Guess I need to replace that, too.
Thanks Travis, I will certainly look into getting that book !
Also,
I'm still having problems, sorry but the string is supposed to look as follows:
my $string = "abc#ccd#egg#hij#klm#nop#rst#luv#wxyx#";
The string is not a fixed length, it may be longer than this but the pattern will always be the same - to ignore all characters before the 5th Hash, in this example I want to extract the following:
#nop#rst#luv#wxyx#
Any help will be greatly appreciated.
Thanks.
Well, to remove the everything before the fifth pound...
Like I said, I don't think you need to escape the pounds, but it shouldn't break anything if you do.Code:$string =~ s/^(\#.*){4}\#/\#/;
# or
$string =~ s/^(#.*){4}#/#/;
Now this will modify the string, so you will loose everything at the beginning of the string. There are way around that. You could copy the string and work the with copy.
Another idea is....
Code:my $string; #the pound enriched string
my @subStrings; #each element is a peice of $string without the #'s
my $result; #the resulting string
my $targetPound; #this is the index of the #s in the string that you want to start reading. 0 is the first
@subStrings = split /#/, $string;
$result = join /#/, $subStrings[$targetPound .. -1]