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?

【Swift】競プロ用正規表現が意外と色々なところで使えそう|自分用メモ【LeetCode/AtCoder】

0
Last updated at Posted at 2025-12-22

文字列周りの問題に関する過去投稿記事の中で、正規表現を用いてACになった問題があるのだが、それが色々な問題に応用が利きそうなのでいつでもコピペ出来るようにしたい。

enum ReplacementMode {
    case replace
}

var text = "" //検索したい文字列を入れる
let mode: ReplacementMode = .replace
let pattern = "^(dreamer|dream|eraser|erase)*$"
if let regex = try? NSRegularExpression(pattern: pattern) {
    switch mode {
        case .replace:
            // 該当する文字を置き換える
            text = regex.stringByReplacingMatches(in: text, range: NSRange(text.startIndex..., in: text), withTemplate: "")
    }
}

色々なパターンの問題に出会うたびに使うのがよさそう。

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?