Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I thought I already had. :-) But OK, let’s add an /x modifier to the regex to make it easier to break apart:

$found =~ s/ ENV{\" # match the characters: ENV{" (.*?) # followed by any characters -- this sequence +is the "value", and the parentheses capture it into the special varia +ble $1 \"} # followed by: "} / \$ENV( # and substitute a literal: $ENV(" $1 # followed by the captured "value" \") # followed by: ") /gx; # and repeat the search & replacement to the e +nd of $found

Notes:

  • The double quotes are backslashed only because I was using a one-liner (and I’m on Windows). This is unnecessary in a .pl script. (But the $ must be backslashed to show that it is literal, not the special symbol for “match the end of a line” (in the search part) or a sigil denoting a scalar variable to be interpolated (in the replacement part). Update: Added the words from “(in the search part)” to the end of the sentence.)

  • The ? in the capture group (.*?) makes the match non-greedy. So the regex engine looks for the fewest number of characters occurring between ENV{" and "}. Without the ?, the match would be greedy, and on the string:

    ENV{"VCINSTALLDIR"}\ATLMFC\LIB\amd64;ENV{"LIBPATH"}

    it would match

    VCINSTALLDIR"}\ATLMFC\LIB\amd64;ENV{"LIBPATH
  • which is not what we want!

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^4: Search and replace by Athanasius
in thread Search and replace by Bharath666

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2014-09-18 04:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (108 votes), past polls