Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: How to replace on a smooth way?

by Ratazong (Monsignor)
on Apr 19, 2012 at 14:04 UTC ( #965968=note: print w/replies, xml ) Need Help??

in reply to How to replace on a smooth way?

$s =~ s/(§[\S]*)/\[$1\]/g;
You search for a §, followed with anything but a blank and store it to $1. Then you replace it by itself surrounded by brackets. The modifier g means that you do it as often as possible.

Update: Thanks to i5513 and MidLifeXis for their improvements of my regex and for polishing my rusty knowledge! I like that in PerlMonks: by answering questions you often learn something for yourself, too :-)

Replies are listed 'Best First'.
Re^2: How to replace on a smooth way?
by MidLifeXis (Monsignor) on Apr 19, 2012 at 14:37 UTC

    In addition to i5513's response, you do not need to place \S within brackets — it is already a character class.


Re^2: How to replace on a smooth way?
by i5513 (Pilgrim) on Apr 19, 2012 at 14:25 UTC
    Backslashes can be omited in the second operator of the substitution, there is not need to scape brackets there.
Re^2: How to replace on a smooth way?
by bestsiteeditor (Initiate) on Apr 19, 2012 at 14:18 UTC
    Thank you so much, but here comes the tricky part: I have to match "§" and in the replacement to use "& # 0167;" otherwise - either does not match for "& # 0167;", or replacing with "§" in it - brings strange prints

      The stuff within the parens is a capture - if you move the § outside of the capturing parenthesis, and then add your required entity to the replacement text, it should do what you need.

      So, if you have a replacement s/(A\S+)/[$1]/ and want to replace A with something else, you would use s/A(\S+)/[B$1]/ instead, replacing A and B as necessary.

      A read of perlre, perlrequick, and perlretut may be helpful. I also wonder if HTML::Entities may be a more appropriate solution to the second requirement.



      That sounds like you may have an encoding problem, then.


      When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://965968]
[erix]: that's a good one. I must remember that :P
[stevieb]: I really dislike how perlbrew doesn't list your version of perl if a newer minor version bump has happened. eg: I use 5.24.1, but 5.24.2 is available, hiding my 5.24.1. I may look at this, as I made it display correctly in berrybrew in cases like this
[stevieb]: choroba just gleaned your post about Module::Starter. I use it too, pretty much for every dist I write

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-08-18 20:48 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.