Replace words into a string with a space before or after

F

Francesco Moi

Hello.

I've got a string with some words inside, and I'm trying to replace
'select' to 'choose'.

---------------------------------------------
$mytext = "select push enter selected";
$mytext =~ s/select/choose/g;
print $mytext;
-----------------------------------------------

But sometimes, my string can content 'selected' and it's replace
to 'choosed'.

I would like to replace only the words 'select ' or ' selected' (note
the space after and before 'select'), but I'm not able to find the
way using regexps.

Does anybody have any experience? Thank you very much, and best regards.
 
B

Brian Wakem

Francesco Moi said:
Hello.

I've got a string with some words inside, and I'm trying to replace
'select' to 'choose'.

---------------------------------------------
$mytext = "select push enter selected";
$mytext =~ s/select/choose/g;
print $mytext;
-----------------------------------------------

But sometimes, my string can content 'selected' and it's replace
to 'choosed'.

I would like to replace only the words 'select ' or ' selected' (note
the space after and before 'select'), but I'm not able to find the
way using regexps.

Does anybody have any experience? Thank you very much, and best regards.


The question makes no sense and you contradict yourself, but I think you
need to be using word boundaries.

$mytext =~ s/\bselect\b/choose/g;
 
P

PapaBear

Brian Wakem said:
The question makes no sense and you contradict yourself, but I think you
need to be using word boundaries.

$mytext =~ s/\bselect\b/choose/g;

And perhaps, using the same word boundaries, you might consider replacing
selected by chosen first if you want to maintain proper syntax ;>)

$mytext =~ s/\bselected\b/chosen/g;
....

___________________________________
Never mind the Bear, beware of papa...

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GAT/CS/CM/O d+(--) s++:+>: a? C++(+++)$ UL++(+++) P++>+++ L++>+++ E- W+++$
N++ !o !K w !O M- V? PS->$ PE+(-) Y+ PGP t+ 5? !X R- tv b+(+++) DI? !D G(-)
!e h---- r+++ y?
------END GEEK CODE BLOCK------
http://www.geekcode.com
 
T

Tad McClellan

Francesco Moi said:
I'm trying to replace
'select' to 'choose'.
$mytext =~ s/select/choose/g;
But sometimes, my string can content 'selected' and it's replace
to 'choosed'.

I would like to replace only the words 'select '


So 'preselect ' should become 'prechoose ' ?

or ' selected'


And don't you want ' chosen' rather than ' choose' substituted for that one?

Does anybody have any experience?


Use a "word boundary":

$mytext =~ s/\bselect\b/choose/g;
$mytext =~ s/\bselected\b/chosen/g;
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top