Use Ruby to write HTML scripts to replace the example sharing of applets.
A file contains the following content:
<P style = "display: none"> select D for this question ............. </P>
The function I want to implement is to replace it:
<Div style = "display: none" class = "sl_explain"> select D ................ </Div>
This seems a bit simple, but it took me half a day to implement this function, mainly because I haven't written RUBY programs for a long time, so I am unfamiliar with the API. Secondly, I am familiar with regular expressions, in particular, ruby is not familiar with regular expressions. Finally, some details are not enough.
To implement the above functions, You can take two steps. The first step is
<P style = "display: none"> select D for this question ............. </P>
Replace \ n:
<P style = "display: none"> select D for this question ............. </P>
In this form, why should we replace \ n? Because the reading file requires a row to be read, if \ n exists, this row cannot be read, so when matching with a regular expression, naturally, the matching is incomplete. To replace and replace
<P style = "display: none"> select D for this question ............. </P>
The internal \ n requires some restrictions. The specific implementation code is as follows:
File. open ("logical padding 2.htm", "w") do | test |
File. open ("logical fill .htm", 'r: gbk') do | file. each_line do | line | if (line. start_with? ('<P style = "display: none"> ')&&! Line. end_with? ("</P> \ n") line. gsub! (Regexp. new ('\ n'), '') end test. print line endendend
Put the replaced content in the new file logical logic to fill in 2.html "(Note 1: When the file is output above, print is used instead of puts. Otherwise, it will naturally add a \ n, so it will be replaced in white; Note 2, the above end_with is followed by \ n, because at the end of the line, there is an invisible line break \ n; Note 3, sometimes <p style = "display: none "> there is a space in front, so you can change start_with to include ?), Then read the file again, and then use the regular expression to replace it, and put the replaced content in “test.html:
File. open ("test.html", "w") do | test | File. open ("logical padding 2.htm", 'R') do | file. each_line do | line. gsub! (Regexp. new ('<p style = "display: none"> (. *) </p> '),' <div style = "display: none" class = "sl_explain"> \ 1 </div> ') test. puts line endendend
In this way, the functions I want to implement are achieved. In addition, if the file is not read in one row, you can use multi-row matching to do the following:
Regexp.new('<p style="display:none">(.*)</p>',Regexp::MULTILINE)
Unfortunately, I only came up with a row-by-row reading method, so the multi-row matching mode is useless.