in reply to remove words with regex
Do you really get John, if it's there?
$str =~s/.*(John).*/$1/;
Do you want to keep the last string after the last slash and before the dot?
$str =~s{.* / | \. .*}{}gx;
Or, maybe, you want the fist word after the fifth slash?
$str =~s{ (?: [^/]* / ){5} ( \w+ ) .* }{$1}x;
Or, ...
Also, do you really need to use substitution? Matching seems more appropriate.
print $str =~ /(John)/; print $str =~ m{ .* / (.+) \. }x; print $str =~ m{ (?: [^/]* / ){5} ( \w+ ) }x;
Do you need to use regexes at all?
my $s = 'John'; print $s if -1 != index $str, $s;
Update: Some alternatives provided.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: remove words with regex
by frank1 (Monk) on Dec 20, 2024 at 09:31 UTC |