Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to replace on a smooth way?

by bestsiteeditor (Initiate)
on Apr 19, 2012 at 13:51 UTC ( #965961=perlquestion: print w/ replies, xml ) Need Help??
bestsiteeditor has asked for the wisdom of the Perl Monks concerning the following question:

I need smooth solution to replace each instance in a string that starts with § with the same instance surrounded with instance

for example the string

"§14.2. done -!! this direction was in contradiction with §8.4. §14.4. Sorry, cannot get the logic - when"

should become :

§14.2. done -!! this direction was in contradiction with §8.4. §14.4. Sorry, cannot get the logic - when"

In a nutshel - anything that starts with § to the first white space should be surrounded with tags.

Comment on How to replace on a smooth way?
Re: How to replace on a smooth way?
by Ratazong (Prior) on Apr 19, 2012 at 14:04 UTC

    $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 :-)
      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.

        --MidLifeXis

        bestsiteeditor:

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

        ...roboticus

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

      Hello,
      Backslashes can be omited in the second operator of the substitution, there is not need to scape brackets there.

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

      --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965961]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2013-05-21 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (447 votes), past polls