in reply to remove words with regex

You didn't describe the problem in sufficient detail.

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.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: remove words with regex
by frank1 (Monk) on Dec 20, 2024 at 09:31 UTC

    thank you