Re: Prepend + to search words



"John W. Krahn" schreef:
Grant:
?:
?:

Hello, how can I prepend a "+" character to each of the words in a
string? For example, "big blue widgets" should be changed to "+big
+blue +widgets".

I'd probably use an s/// (substitution) on the string. Match a word,
replace it with the plus sign and the word. Have you tried that?

I just had a look at some docs matching "perl substitution variable"
since the words in the string could be anything, but perl docs have a
way of overwhelming me really quickly. Can you show me how?

$ perl -le'$_ = "big blue widgets"; print; s/(\w+)/+$1/g; print'
big blue widgets
+big +blue +widgets


Alternative without capturing:

$ perl -wle'
$_ = q{big blue widgets};
print;
s/\b(?=\w)/+/g;
print
'
(see perlretut and perlre)

--
Affijn, Ruud

"Gewoon is een tijger."

.



Relevant Pages

  • Re: Prepend + to search words
    ... I'd probably use an s/// (substitution) on the string. ... I just had a look at some docs matching "perl substitution variable" ...
    (perl.beginners)
  • Re: Escape $ in replace pattern: How to replace pattern "a*c" by exact string "p$1q&q
    ... > jakarta ORO Regex package which uses Perl5 internally. ... > a perl pattern for the search string and replace string. ... (I am assuming here that another similar substitution did actually ...
    (comp.lang.perl.misc)
  • Re: Need help editing a string
    ... I want to delete the last character in the string if it is a '/'. ... This is basically a pattern matching and substitution problem. ... The book I mostly learned perl from is called Little Black Book, ...
    (perl.beginners)
  • Re: Question on regex substitution using variables...
    ... back-substitution, and just prints "$1.def". ... Once you have put a literal $ into a string, ... substitution as a little code block and have it evaluated: ... This being Perl, I await news of the 1867 other ways to do it... ...
    (comp.lang.perl)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)