Python | レベル別練習問題:正規表現

Python
スポンサーリンク

では Python 初心者向け「正規表現(re)」練習問題 50問+解答・解説付きセット を紹介します。レベル別(基礎 → 中級 → 応用)で構成し、実務でよく出るパターンも含めます。すぐ Python で実行できる形式にします。

レベル1:基礎(1〜15問) — 正規表現の書き方・検索・抽出

Q1. "abc123xyz" から数字だけを抜き出す正規表現を書きなさい。
解答例:

import re
s = "abc123xyz"
print(re.findall(r"\d+", s))  # ['123']
Python

解説: \d+ は 1 回以上の数字にマッチします。


Q2. "apple, banana, cherry" をカンマで分割する正規表現を書きなさい。

s = "apple, banana, cherry"
print(re.split(r",\s*", s))  # ['apple','banana','cherry']
Python

解説: \s* は 0 個以上の空白にマッチし、空白を無視して分割。


Q3. "The price is $123.45" から $ を含む数字を抜き出す。

s = "The price is $123.45"
print(re.findall(r"\$\d+(\.\d+)?", s))  # ['$123.45']
Python

解説: (\.\d+)? は小数点以下がある場合に対応。


Q4. "cat, dog, bird" に対して単語単位で検索する正規表現を書きなさい。

s = "cat, dog, bird"
print(re.findall(r"\b\w+\b", s))  # ['cat','dog','bird']
Python

解説: \b は単語境界。\w+ は単語文字 1 回以上。


Q5. "abc123xyz456" から数字の後ろに続く文字列を抽出する。

s = "abc123xyz456"
print(re.findall(r"\d+([a-z]+)", s))  # ['xyz']
Python

解説: 数字の後の文字列を括弧 () でキャプチャ。


Q6. 行頭が # の行を検索する正規表現を書きなさい(複数行)。

text = "#comment\nline2\n#note"
print(re.findall(r"^#.*", text, re.M))  # ['#comment','#note']
Python

解説: ^ 行頭、.* 任意文字列、re.M マルチライン。


Q7. "Hello World" から大文字のアルファベットを抜き出す。

s = "Hello World"
print(re.findall(r"[A-Z]", s))  # ['H','W']
Python

Q8. "abc123xyz" から英字だけを抜き出す。

s = "abc123xyz"
print(re.findall(r"[a-zA-Z]+", s))  # ['abc','xyz']
Python

Q9. " hello " から前後の空白を取り除く正規表現を書きなさい。

s = "   hello   "
print(re.sub(r"^\s+|\s+$", "", s))  # 'hello'
Python

Q10. "a1b2c3" の数字をすべて # に置換する。

s = "a1b2c3"
print(re.sub(r"\d", "#", s))  # 'a#b#c#'
Python

Q11. "2025-11-05" から年月日をそれぞれキャプチャして出力。

s = "2025-11-05"
m = re.match(r"(\d{4})-(\d{2})-(\d{2})", s)
print(m.groups())  # ('2025','11','05')
Python

Q12. "foo_bar"_ を含むか検索。

s = "foo_bar"
print(re.search(r"_", s) is not None)  # True
Python

Q13. "abc123" が数字で終わるか確認する。

s = "abc123"
print(bool(re.search(r"\d$", s)))  # True
Python

Q14. "abcXYZ" から小文字だけを抜き出す。

s = "abcXYZ"
print(re.findall(r"[a-z]", s))  # ['a','b','c']
Python

Q15. "cat dog bird" の単語数を数える正規表現を書きなさい。

s = "cat dog bird"
print(len(re.findall(r"\b\w+\b", s)))  # 3
Python

レベル2:中級(16〜35問) — 条件付き抽出・置換・グループ化

Q16. "taro@example.com, hanako@sample.co.jp" からメールアドレスをすべて抜き出す。

s = "taro@example.com, hanako@sample.co.jp"
print(re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", s))
Python

Q17. "090-1234-5678" の電話番号を抽出(簡易日本パターン)。

s = "090-1234-5678"
print(re.findall(r"\b0\d{1,4}-\d{1,4}-\d{4}\b", s))
Python

Q18. "abc123xyz456" の数字部分をすべて整数型のリストに変換する。

s = "abc123xyz456"
nums = [int(x) for x in re.findall(r"\d+", s)]
Python

Q19. "apple,banana,cherry" のカンマをセミコロンに置換する。

s = "apple,banana,cherry"
s2 = re.sub(r",", ";", s)
Python

Q20. "http://example.com" の URL を抽出する簡易正規表現。

s = "http://example.com"
re.findall(r"https?://[^\s]+", s)  # ['http://example.com']
Python

Q21. "Price: $12, Tax: $1"$ を取り除いた数字だけ抽出。

s = "Price: $12, Tax: $1"
nums = [int(x) for x in re.findall(r"\d+", s)]  # [12,1]
Python

Q22. " foo bar " の両端の空白を削除して単語リストにする。

s = "   foo   bar "
words = re.findall(r"\b\w+\b", s)  # ['foo','bar']
Python

Q23. "ab123cd456ef" の 2 桁以上の数字を抽出。

s = "ab123cd456ef"
re.findall(r"\d{2,}", s)  # ['123','456']
Python

Q24. "apple Banana cherry" を大文字小文字無視で "banana" があるかチェック。

s = "apple Banana cherry"
bool(re.search(r"banana", s, re.I))  # True
Python

Q25. "a1b2c3" のアルファベットと数字を分けてリスト化する。

s = "a1b2c3"
letters = re.findall(r"[a-zA-Z]", s)  # ['a','b','c']
digits  = re.findall(r"\d", s)        # ['1','2','3']
Python

レベル2:中級(16〜35問) — 実務向け正規表現

Q16. "taro@example.com, hanako@sample.co.jp" からメールアドレスをすべて抽出せよ。

import re
s = "taro@example.com, hanako@sample.co.jp"
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", s)
print(emails)  # ['taro@example.com', 'hanako@sample.co.jp']
Python

解説: [A-Za-z0-9._%+-]+ はユーザー名、@ は必須、ドメイン名は [A-Za-z0-9.-]+、最後にドットと TLD。


Q17. "090-1234-5678" の日本の電話番号(携帯・固定)を抽出せよ。

s = "連絡先: 03-1234-5678, 携帯: 090-9876-5432"
phones = re.findall(r"\b0\d{1,4}-\d{1,4}-\d{4}\b", s)
print(phones)  # ['03-1234-5678', '090-9876-5432']
Python

解説: 0\d{1,4} → 市外局番や携帯の最初、- で区切り、最後は4桁。


Q18. "2025-11-05, 2025/12/31" の日付(YYYY-MM-DD または YYYY/MM/DD)を抽出せよ。

s = "2025-11-05, 2025/12/31"
dates = re.findall(r"\d{4}[-/]\d{2}[-/]\d{2}", s)
print(dates)  # ['2025-11-05', '2025/12/31']
Python

解説: [-/] でハイフンまたはスラッシュに対応。


Q19. "〒123-4567" の郵便番号を抽出せよ。

s = "〒123-4567"
zipcode = re.findall(r"\d{3}-\d{4}", s)
print(zipcode)  # ['123-4567']
Python

解説: \d{3}-\d{4} で3桁-4桁形式。


Q20. "Name: Taro, Age: 28" から年齢(数字)だけを取り出せ。

s = "Name: Taro, Age: 28"
age = re.findall(r"Age:\s*(\d+)", s)
print(age)  # ['28']
Python

Q21. "http://example.com, https://secure.example.com" から URL を抽出せよ。

s = "http://example.com, https://secure.example.com"
urls = re.findall(r"https?://[^\s,]+", s)
print(urls)  # ['http://example.com', 'https://secure.example.com']
Python

Q22. "abc123xyz456" の数字をすべて整数リストに変換せよ。

s = "abc123xyz456"
nums = [int(x) for x in re.findall(r"\d+", s)]
print(nums)  # [123, 456]
Python

Q23. "apple,banana , cherry" のカンマ区切りをセミコロン区切りに置換せよ(空白を無視)。

s = "apple,banana , cherry"
s2 = re.sub(r",\s*", ";", s)
print(s2)  # 'apple;banana;cherry'
Python

Q24. "abcXYZ123" の英字だけ抽出せよ。

s = "abcXYZ123"
letters = re.findall(r"[a-zA-Z]+", s)
print(letters)  # ['abc','XYZ']
Python

Q25. "foo bar baz" の単語数を数えよ。

s = "foo  bar   baz"
words = re.findall(r"\b\w+\b", s)
print(len(words))  # 3
Python

Q26. "Tel: (03)1234-5678" の電話番号から市外局番(括弧内)だけ抽出せよ。

s = "Tel: (03)1234-5678"
area = re.findall(r"\((\d+)\)", s)
print(area)  # ['03']
Python

Q27. "user1@example.com;user2@test.co.jp" のメールアドレスをセミコロンで分割せずに抽出。

s = "user1@example.com;user2@test.co.jp"
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", s)
print(emails)  # ['user1@example.com', 'user2@test.co.jp']
Python

Q28. "abc-def-ghi" の文字列中のハイフンをすべて取り除け。

s = "abc-def-ghi"
s2 = re.sub(r"-", "", s)
print(s2)  # 'abcdefghi'
Python

Q29. "2025/11/05, 2025-12-31" の日付を全て YYYYMMDD 形式に変換せよ。

s = "2025/11/05, 2025-12-31"
dates = re.findall(r"(\d{4})[-/](\d{2})[-/](\d{2})", s)
dates_new = ["".join(t) for t in dates]
print(dates_new)  # ['20251105','20251231']
Python

Q30. "abc123def456" の数字だけを置換して # に変えよ。

s = "abc123def456"
s2 = re.sub(r"\d", "#", s)
print(s2)  # 'abc###def###'
Python

Q31. "apple Banana cherry" に大文字小文字無視で "banana" があるかチェックせよ。

s = "apple Banana cherry"
print(bool(re.search(r"banana", s, re.I)))  # True
Python

Q32. " foo bar " の両端の空白を削除せよ。

s = "  foo  bar "
s2 = re.sub(r"^\s+|\s+$", "", s)
print(s2)  # 'foo  bar'
Python

Q33. "abcXYZ123" の数字だけをリスト化せよ。

s = "abcXYZ123"
digits = re.findall(r"\d+", s)
print(digits)  # ['123']
Python

Q34. "item1,item2;item3 item4" をカンマ・セミコロン・空白で分割せよ。

s = "item1,item2;item3 item4"
items = re.split(r"[,\s;]+", s)
print(items)  # ['item1','item2','item3','item4']
Python

Q35. "Price: $12.50, Tax: $1.25" から小数も含めて金額だけ抽出せよ。

s = "Price: $12.50, Tax: $1.25"
prices = re.findall(r"\$\d+(?:\.\d+)?", s)
print(prices)  # ['$12.50','$1.25']
Python

レベル3:応用(36〜50問) — ログ・HTML・条件付き抽出など

Q36. "Error: File not found, Warning: Disk full" から "Error""Warning" のみ抽出せよ。

import re
s = "Error: File not found, Warning: Disk full"
msgs = re.findall(r"\b(Error|Warning)\b", s)
print(msgs)  # ['Error', 'Warning']
Python

解説: () でキャプチャ、| で OR 条件。


Q37. "abc123xyz456" の数字部分だけを合計せよ。

s = "abc123xyz456"
total = sum(int(x) for x in re.findall(r"\d+", s))
print(total)  # 579
Python

Q38. HTML の簡単なタグ <p>Hello</p><div>World</div> の中身だけ抽出せよ(属性無視)。

html = "<p>Hello</p><div>World</div>"
contents = re.findall(r"<([a-zA-Z][a-zA-Z0-9]*)>(.*?)</\1>", html)
print(contents)  # [('p','Hello'), ('div','World')]
Python

解説: \1 で前のグループ(タグ名)を参照。


Q39. "2025-11-05, 2025-12-31" の日付を年・月・日別のタプルリストにせよ。

s = "2025-11-05, 2025-12-31"
dates = re.findall(r"(\d{4})-(\d{2})-(\d{2})", s)
print(dates)  # [('2025','11','05'),('2025','12','31')]
Python

Q40. "abc123def456ghi789" の数字部分を #数字# に置換せよ。

s = "abc123def456ghi789"
s2 = re.sub(r"(\d+)", r"#\1#", s)
print(s2)  # 'abc#123#def#456#ghi#789'
Python

Q41. "user1@example.com, user2@test.co.jp"; 区切りで出力せよ。

s = "user1@example.com, user2@test.co.jp"
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", s)
print(";".join(emails))  # 'user1@example.com;user2@test.co.jp'
Python

Q42. "abc def ghi" のすべての単語の最初の文字を大文字にせよ。

s = "abc def ghi"
s2 = re.sub(r"\b(\w)", lambda m: m.group(1).upper(), s)
print(s2)  # 'Abc Def Ghi'
Python

解説: キャプチャと lambda で置換。


Q43. "abc123def456ghi789" の数字を 2 倍して置換せよ。

s = "abc123def456ghi789"
s2 = re.sub(r"\d+", lambda m: str(int(m.group())*2), s)
print(s2)  # 'abc246def912ghi1578'
Python

Q44. 複数行テキスト "line1\n#comment\nline2" からコメント行(#で始まる行)を除去せよ。

text = "line1\n#comment\nline2"
clean = "\n".join([ln for ln in text.splitlines() if not re.match(r"^\s*#", ln)])
print(clean)  # 'line1\nline2'
Python

Q45. "abc123xyz456" の数字の前後に角括弧 [ ] を付けよ。

s = "abc123xyz456"
s2 = re.sub(r"(\d+)", r"[\1]", s)
print(s2)  # 'abc[123]xyz[456]'
Python

Q46. "http://a.com https://b.jp ftp://c.net" から httphttps の URL のみ抽出せよ。

s = "http://a.com https://b.jp ftp://c.net"
urls = re.findall(r"https?://[^\s]+", s)
print(urls)  # ['http://a.com','https://b.jp']
Python

Q47. "abc123def456" の数字部分を逆順でリスト化せよ。

s = "abc123def456"
nums = re.findall(r"\d+", s)[::-1]
print(nums)  # ['456','123']
Python

Q48. "商品A: 123円, 商品B: 45円" の数字だけを取り出し、合計せよ。

s = "商品A: 123円, 商品B: 45円"
total = sum(int(x) for x in re.findall(r"\d+", s))
print(total)  # 168
Python

Q49. "abcXYZ123" の小文字をすべて大文字に変換せよ(正規表現で)。

s = "abcXYZ123"
s2 = re.sub(r"[a-z]", lambda m: m.group().upper(), s)
print(s2)  # 'ABCXYZ123'
Python

Q50. "2025/11/05" の日付を "2025年11月05日" 形式に変換せよ。

s = "2025/11/05"
s2 = re.sub(r"(\d{4})/(\d{2})/(\d{2})", r"\1\2\3日", s)
print(s2)  # '2025年11月05日'
Python

Python
スポンサーリンク
シェアする
@lifehackerをフォローする
スポンサーリンク
タイトルとURLをコピーしました