Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Your source string needs to be in single quotes, otherwise Perl will try to interpolate '@begin' and '@end' as arrays. You also don't want to put your replacement strings inside square brackets, that groups them as a single array reference inside of @replace, rather than as individual strings.

Anyway, this should get you started:

use strict; use warnings; my $string=' hi hello @begintobe replace @end asas x x x x x zxzxax hi hello hi hellooo @beginthis has tobe replaced @end ...'; my @replace= ("replacestring1","replacestring2"); $string =~ s|\@begin.*?\@end |shift @replace // '[null]' |sexg; print $string;

Usually regular expression substitutions are written as:

s/<pattern>/<substitution text>/<modifiers>

But in my code above, the vertical bar (or pipe) symbol (|) is used instead. It's also split over 3 lines which is allowed when you use the "x" modifier.

The "e" modifier means that instead of substitution text, the given code will be executed for each @begin ... @end match. The code shown, removes the first element of the array and returns it for the current substitution. If the @array becomes empty, the literal value '[null]' will be used instead.

The "g" modifier causes the substitution to be done as many times as there are matches found in the string -- consuming another array element each time. Without it only the first substitution would be done.

And lastly the "s" modifier means that the period character in the pattern, will match newline characters since those appear in your example input string.

Much more information can be found in the perlre and perlretut documentation.


In reply to Re: need a regular exdpression in perl by Loops
in thread need a regular exdpression in perl by praveenchappa

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found