Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, the / in the closing is being treated as an end-quote to the regular expression. Either backslash it:</p> <pre><code>$a =~ s/&lt;no&gt;(.*)&lt;\/no&gt;/000/gi; </code></pre> <p>or use a different character to / in your regex:</p> <pre><code>$a =~ s~&lt;no&gt;(.*)&lt;/no&gt;~000~gi; </code></pre> <p>Secondly, I'm guessing you're trying to parse an XML document with this and change data. I'm also guessing that you have <em>many</em> <code>&lt;no&gt;</code>...<code>&lt;/no&gt;</code> sections in your document. The problem with the regular expression you gave is that the <code>(.*)</code> will match <em>as much as possible</em>, i.e. everything between the <em>first</em> <code>&lt;no&gt;</code> and the <em>last</em> <code>&lt;/no&gt;</code> in your document, <em>including</em> any other tags in between. It <em>also</em> replaces the <code>&lt;no&gt;</code> and <code>&lt;/no&gt;</code>.</p> <p>You can use a non-greedy match, that is one that will match <em>as little as possible</em>. You can put a question mark after the * like so:</p> <pre><code>$a =~ s~&lt;no&gt;(.*?)&lt;/no&gt;~000~gi; </code></pre> <p>Since this still replaces the <code>&lt;no&gt;</code>...<code>&lt;/no&gt;</code>, you will probably want to put those back in:</p> <pre><code>$a =~ s~&lt;no&gt;(.*?)&lt;/no&gt;~&lt;no&gt;000&lt;/no&gt;~gi; </code></pre> <p>In the case where your <code>&lt;no&gt;</code> is instead a regular expression, you can't just put it into your substitution string. You can either use lookarounds as suggested by others, or just capture it and put it back in using $1..$9, like so:</p> <pre><code>$a =~ s~(&lt;no&gt;)(.*?)(&lt;/no&gt;)~$1000$3~gi; </code></pre> <p>Why $3? Because $2 is whatever you captured with <code>(.*?)</code>. Of course, since you don't actually care about what you've captured, you can just do this:</p> <pre><code>$a =~ s~(&lt;no&gt;).*?(&lt;/no&gt;)~$1000$2~gi; </code></pre> <p>which is probably about as efficient as you're going to get for this problem.</p> <p>As an aside, it is normally a bad idea to try to parse XML with regular expressions, because XML is too varied for regular expressions to parse. I quite like <code>XML::LibXML</code> for processing XML documents, but it is not at all simple to get in to. However, if you are confident about the precise format of your XML (or in fact it's not XML but just looks a bit like it) then regular expressions are OK as a local hack.</p> <p>This is all covered in the <code>perlre</code> manpage, which is a must-read if you're going to do anything even remotely non-trivial with Perl regular expressions.</p> <pre><code>$ perldoc perlre </code></pre> <p>Hope all the examples help clarify things a bit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload