LoginSignup
4
3

More than 3 years have passed since last update.

面倒な正規表現をRubularで簡単に

Last updated at Posted at 2020-12-30

!macOS-11.1 !ruby-2.7.2p137

Preface (はじめに)

欲しい文字列を抽出するのに便利なのが正規表現です.
しかし, 正規表現は覚えることが多く, 使いこなすのに手間がかかります.

そこでRubularというRubyでの正規表現をしてくれるツールが役に立ちます.

rubular

上のYour regular expressioは正規表現を入れる場所.

左のYour test stringは文字列を, 右には抽出結果が出ます.

下にあるRegex quick referenceは正規表現の例がいくつか示されています.

Example

まず, test文字列

#+qiita_id: helloworld

Your test stringに入れます.

では, ここからhelloworldを取り出します.
Your regular expression

(.+)

と打ちます.これはany single character を表す.と, 一つ以上を表す+を,
capture everything enclosed を表す(...)で囲います.すると,

  • Match result:
#+qiita_id: helloworld
  • Match groups:
1. #+qiita_id: helloworld

helloworldだけを取り出すには

:(.+)

で, できます.

正規表現はなかなか覚えにくいので, rubular をつかって以後も学んでいこうと思います.

参考資料

チャート式ruby-appendix-V(rubular)

4
3
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
4
3