Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl regular expression - search and replace
    text
    copied!<p>I can't get my head around perl search and replace in a string, or regular expressions. Can someone help, please?</p> <p>I've got a file that contains HTML and I want to change the class of certain items if they a present in given set (in this case a hash table or validated fields that failed validation). So I've got, for example, the following:</p> <pre><code>&lt;input name="RMAProcess" type="radio" class="inputnormal" value="Good"&gt; </code></pre> <p>and I want to change the "inputnormal" class to the value "inputpink". What I'm trying to do is make it so that I match the whole string nomatter in which order the properties appear, so that, for example, the following will also match:</p> <pre><code>&lt;input name="RMAProcess" type="radio" value="Good" class="inputnormal"&gt; </code></pre> <p>This is because Dreamweaver will sometimes mess around with the order of properties in the HTML during editing, so I can't rely on fixed positions for them.</p> <p>So I end up with something that doesn't work, such as:</p> <pre><code>foreach $key ( keys %FAILED ) { $infile =~ s/&lt;input type="radio" name="$key" value="$VALUES{$key}" class="([a-zA-Z]+)"/$1inputpink"/gi; } </code></pre> <p>Can anyone help me out with this?</p> <p>Thanks.</p> <p>Edit: ok, this kind-of works, assuming the properties inside the tags don't move around:</p> <pre><code>foreach $key ( keys %FAILED ) { $infile =~ s/(&lt;.{4,10}\ type="radio" name="$key".{1,60}class=")([a-zA-Z]+)"/$1inputpink"/gi; } </code></pre> <p>That will have to do for now! Thanks for all your responses.</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