0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

正規表現の特訓 1

Posted at

はじめに

正規表現が苦手なので
基礎を学ぶために時々練習することにしました。
今回は、HTMLのタグを用いて練習します。


問題1

HTML 文字列から、すべての 開始タグ (<タグ名> 形式) を抽出するコード

my $html = <<__HTML;
<head>
  <meta charset="utf-8">
  <title>TEST! TEST</title>
  <meta name="description" content="testtest">
  <script src="test.js"></script>
</head>
__HTML

my @tags = ($html =~ /<([a-z]+)[\sa-z\-"\.=]*>/g);
foreach my $tag (@tags){
  print '<' . $tag . '>' . "\n";
}

# => 出力結果
<head>
<title>
<meta>
<script>

問題2

HTML 文字列から、属性を含む開始タグ全体 を抽出するコード

my @tags = ($html =~ /<([a-z]+[\sa-z\-"\.=]*)>/g);
foreach my $tag (@tags){
  print '<' . $tag . '>' . "\n";
}

# => 出力結果
<head>
<title>
<meta name="description" content="testtest">
<script src="test.js">

問題3

タグとその中身をセットで抽出するコード

my @tags = ($html =~ /(<[a-z]+[\sa-z\-"\.=]*>.*?<\/[a-z]+>)/g);
foreach my $tag (@tags){
  print $tag . "\n";
}

=> 
<title>TEST! TEST</title>
<script src="test.js"></script>

まとめ

少しずつ練習していこうと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?