myword myobject,option2,option3 word word wordThe words delimited by comma after myobject are optional. There may or may not be these optional words in each line.
while (my $line=) {
if $line =~ /^myword\s+/ {
work on line;
}
}
I want to strip "myword myobject,option1,option2" from line
but not know how do this e.g. something like
if $line =~ /^myword\s+/ {
$line =~ s/^myword myobject,options//g
}
I knows what first few characters of myword will be but not what myobject will be (except it's always a word).
if $line =~ /^myword.*$/ {
$line =~ s/$&//g
}
if $line =~ /^myword.*$/ {
$line =~ s/$&//g
}
Updated. Oh I see you need to remove the line until the last option. Heres the code:
if $line =~ /(^myword.*option\d+\s+).*$/ {
$line =~ s/$1//g
}
abc123 qwerty,asdfg,zxcvb yuio jklhor like this
abc987 qwerty yuio jklhor this
abcefgh qwerty, asdfg yuio jklhSo I want to end up with same string from all examples above. It should end up like this
yuio jklhHope this make it bit clearer, sorry for confuse my english not so good
s/abc\w+\s+\w+(?:, *\w+)\s+//; # match and remove unwanted initial contentor else just look for what you want to keep at the end:
s/.*\s(\w+\s+\w+)$/$1/; # match desired end content and remove everything before itAnd of course, if you know in advance how those last two tokens are spelled, you could even use rindex() and substr():
$_ = substr( $_, rindex( $_, 'yuio jklh' ));
$line =~ s/^myword \S+//; # strip myword, a space, and any stretch of non-spaces
abcefgh qwerty, asdfg yuio jklhIt print out
asdfg yuio jklhI had to add extra slash to your code like this
$line =~ s/^abc\w+\s+\S+(?:,\s*\S+)*//x;otherwise got errors. I hope this was what you mean. I try to change your code to handle last line which don't leave just yuio jklh .
$line =~ s/^(abc)\w+\s+\S+(?:,\s*\S+)*/$1/x;should give you what you want. You were right about the missing slash.
perlmonks.org content © perlmonks.org and Anonymous Monk, graff, idle, Roy Johnson
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03