Reputation: 176
(Looked at other similar questions and answers and got intimidated - and thus ask your forgiveness if this is a redundant and/or too simple of a question.)
Need to extract a substring enclosed by operators, say, <begin>
and <end>
.
Example:
Irrelevant text is here <begin>the substring I am looking for<end> other irrelevant text
For the sake of simplicity, I need only the first match if there are several.
P.S. The string I am extracting is time in the form of 23:59:59;00
which I need then to compare to system time and generate an alert if there is a mismatch by more than a minute, accounting for end of day (e.g. the above time is close enough to 00:00:15;45
and should not be an exception. But that should be a separate question, I guess?
Upvotes: 0
Views: 91
Reputation: 185161
my $text = "Irrelevant text is here <begin>the substring I am looking for<end> other irrelevant text";
my $string = $& if $text =~ /<begin>\K.*?(?=<end>)/;
Upvotes: 1
Reputation: 50647
my $text = "Irrelevant text is here <begin>the substring I am looking for<end> other irrelevant text";
my ($string) = $text =~ /<begin>(.*?)<end>/ or warn "Pattern did not match";
Upvotes: 4