うわーんRubyの

html = "<?xml version=\"1.0\" encoding=\"utf-8\"
?><html lang=\"ja\" prefix=\"og: https://ogp.me/ns#\" xml:lang=\"j
a\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Hello</tit
le></head><body><div class=\"sect1\">\n<h2 id=\"lorem_ipsum\">lore
m ipsum</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\
">\n<p>lorem ipsum</p>\n</div>\n</div>\n</div></body></html>"

html.gsub(/<div class=\\"sect[0-9]*\\">(.*?)(\\n<\/div>){3}/, "<section><h1>hello</h1>this is body<section>")

が書き変わらない理由がわかんないよ〜〜〜

フォロー

@eniehack こうしてみた。

html = %Q!<?xml version="1.0" encoding="utf-8"?><html lang="ja" prefix="og: ogp.me/ns#" xml:lang="ja" xmlns="w3.org/1999/xhtml"><head><title>Hello</title></head><body><div class="sect1">\n<h2 id="lorem_ipsum">lorem ipsum</h2>\n<div class="sectionbody">\n<div class="paragraph">\n<p>lorem ipsum</p>\n</div>\n</div>\n</div></body></html>!

html.gsub(%r!<div class="sect[0-9]*">(.*?)(\n</div>){3}!m, %Q!<section><h1>hello</h1>this is body</section>!)

@eniehack
正規表現にmがないから . が\nにマッチしない、かな。

@eniehack 最終行に改行入るけど、ヒアドキュメントの方が楽かな(脱線)

html = <<EOS
<?xml version="1.0" encoding="utf-8"?><html lang="ja" prefix="og: ogp.me/ns#" xml:lang="ja" xmlns="w3.org/1999/xhtml"><head><title>Hello</title></head><body><div class="sect1">
<h2 id="lorem_ipsum">lorem ipsum</h2>
<div class="sectionbody">
<div class="paragraph">
<p>lorem ipsum</p>
</div>
</div>
</div></body></html>
EOS

Rubyっていろいろなオプションや書き方があるんですね……。教えていただきありがとうございます!

@eniehack 私はRubyの文法好きですねー。ストレスなく楽しく書ける。

実行前にエラーを潰しきれないのがツライので、自分の新規開発はCrystal使ってますがw

確かにRubyはかなり楽しく書けていいですよね。でも、やっぱり実行時エラーを吐くのがつらいですよね……。そしてCrystalは強固な型システムで動的型付け言語っぽくプログラムが書けて、Rubyのような文法なのでいいですよね。メインで使ってみたいなーってずっと思ってます
ログインして会話に参加
Fedibird

様々な目的に使える、日本の汎用マストドンサーバーです。安定した利用環境と、多数の独自機能を提供しています。