I'm not very familiar with regular expressions in perl.
I want to filter names from a string e.g. "child mick jagger child john wayne child archimedes" between tags "child".
Result should be:
mick jagger
john wayne
archimedes
The number of names in the string is variable.
My perl program:
#!/usr/bin/perl
use strict 'vars';
use strict 'subs';
my ($x);
my $s="child mick jagger child john wayne child archimedes";
my @f=$s=~/(child.+(?!child))/igs;
foreach $x (@f)
{
print "$x\n";
};
The program didn't work. Can anybody help?
You might use:
\bchild \K(?:(?!child).)*(?!\S)
\bchild Match child preceded with a word boundary and followed by a space\K Forget what was matched(?:(?!child).)* Match any char except a newline not followed by child(?!\S) Assert what is on the right is not a non whitespace charRegex demo
Or use a non greedy dot variant
\bchild \K.+?(?= child|$)
Regex demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With