🐕

# 正規表現 オンライン教材の基礎編をJulia言語で解いた

2023/03/21に公開

## 解答

``````module TestMatch
using Test

function main()
@testset "カタカナの取り出し" begin
reg_exp = r"\p{Katakana}+"

@test occursin(reg_exp, "ネコ") == true
@test occursin(reg_exp, "犬") == false
@test occursin(reg_exp, "ぞう") == false
@test occursin(reg_exp, "mouse") == false
end

@testset "数字の取り出し" begin
reg_exp = r"\d+"

@test occursin(reg_exp, "一") == false
@test occursin(reg_exp, "二") == false
@test occursin(reg_exp, "3") == true
@test occursin(reg_exp, "4") == true
end

@testset "具体的な数の指定１" begin
reg_exp = r"^bee[a-z]*"

@test occursin(reg_exp, "be") == false
@test occursin(reg_exp, "bee") == true
@test occursin(reg_exp, "beef") == true
@test occursin(reg_exp, "beer") == true
@test occursin(reg_exp, "beech") == true
@test occursin(reg_exp, "beach") == false
end

@testset "繰り返しの練習" begin
reg_exp = r"[A-Z]{4}"

@test occursin(reg_exp, "火星") == false
@test occursin(reg_exp, "SUN") == false
@test occursin(reg_exp, "MOON") == true
@test occursin(reg_exp, "タイタン") == false
@test occursin(reg_exp, "冥王星") == false
end

@testset "最短マッチ" begin
string = "　普通に正規表現を使うと、正規表現のエンジンは「最長マッチ」を行います。これは、「欲張りなマッチ」とか「目一杯のマッチ」とも言われます。下のような例を考えてみましょう。"
reg_exp = r"「.+?」"

@test occursin(reg_exp, string) == true
end

@testset "行頭への一致" begin
reg_exp = r"^This.*"

@test occursin(reg_exp, "This is a pen.") == true
@test occursin(reg_exp, "Is this a pen?") == false
@test occursin(reg_exp, "What is this?") == false
end

@testset "語尾への一致" begin
reg_exp = r".*this\$"

@test occursin(reg_exp, "This is a pen") == false
@test occursin(reg_exp, "Is this a pen") == false
@test occursin(reg_exp, "This is a book") == false
@test occursin(reg_exp, "What is this") == true
end

@testset "単語境界" begin
reg_exp = r"gun\b"

@test occursin(reg_exp, "gun") == true
@test occursin(reg_exp, "gunboat") == false
@test occursin(reg_exp, "gunfight") == false
end

@testset "前方参照" begin
reg_exp = r"和風(?=懐石)"

@test occursin(reg_exp, "和風懐石") == true
@test occursin(reg_exp, "洋風懐石") == false
@test occursin(reg_exp, "洋風御膳") == false
@test occursin(reg_exp, "中華懐石") == false
@test occursin(reg_exp, "中華御膳") == false
end

@testset "or演算子" begin
string = "「この世をば　わが世とぞ思ふ　望月の　欠けたることも　なしと思へば」という唄を詠んだ藤原道長は、藤原道隆、藤原道兼など藤原氏と呼ばれる一族の中でも、とくに有名な人物です。"
reg_exp = r"藤原道長|藤原道隆|藤原道兼"

@test occursin(reg_exp, string) == true
end

@testset "引用記号" begin
reg_exp = r"[ -~]+"

@test occursin(
reg_exp,
"(3.2)*(0.4)/(0.3916)=3.1328のように、プログラミング中で数式を用いる際は通常の数式とは違う書き方を用います。",
) == true
end
end
end

if abspath(PROGRAM_FILE) == @__FILE__
using .TestMatch

TestMatch.main()
end
``````