striped name to variable

B

blnukem

Hi All

How do I strip a word out of a phrase and save the striped part as a
variable example:

$phrase = "My name is Nick<br>";
$phrase =~ s/Nick//is;

Now how do I set a new variable that equals the striped part "Nick"
 
S

Sam Holden

Hi All

How do I strip a word out of a phrase and save the striped part as a
variable example:

$phrase = "My name is Nick<br>";
$phrase =~ s/Nick//is;

Now how do I set a new variable that equals the striped part "Nick"

$new_variable = 'Nick';

Or if we pretend Nick isn't a constant string:

if ($phrase =~ s/(Nick)//is) {
$new_variable = $1;
}
 
P

Paul Lalli

Hi All

How do I strip a word out of a phrase and save the striped part as a
variable example:

$phrase = "My name is Nick<br>";
$phrase =~ s/Nick//is;

Now how do I set a new variable that equals the striped part "Nick"


Save your matches with parentheses, then access them with the $1, $2, $3,
.... variables.

my $name;
if ($phrase =~ s/(Nick)//i){
$name = $1;
}


run `perldoc perlre` and search for 'capture' for more info.

On an unrelated note, why are you using the /s modifier in your RegExp?
It serves no purpose here.

Paul Lalli
 
G

GreenLight

blnukem said:
Hi All

How do I strip a word out of a phrase and save the striped part as a
variable example:

$phrase = "My name is Nick<br>";
$phrase =~ s/Nick//is;

Now how do I set a new variable that equals the striped part "Nick"

my $stripped = ($phrase =~ s/(Nick)//i);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top