in reply to Alternatives for index() ... substr() ?
IMHO the main issue with your code is not the use of index and substr, it's how unhelpful the variable names are. Not only do the name give no clue on their meaning, they look similar enough that you need extra concentration to tell them appart.
Maybe something like my $msg = "the string that comes between" 'copying ' "and" ' from ';? Is there a function that does what the double-quoted text says?Yes, regexes do what you want. You can read the tutorials Pattern Matching, Regular Expressions, and Parsing for more info. Your specific case is quite straightforward:
. means any character, * means several times and the parenthesis capture the result into $1 (another pair of parenthesis would put it in $2, and so on). So overall it's: find "copying", then find several times any character in a row, followed by "from", and put the found characters into $1.$text =~ /copying(.*)from/ or die "Couldn't find the pattern"; my $msg = $1;
|
---|