かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Ruby]正規表現

正規表現といっても、+が1つ以上の繰り返しとかそんなことを書くわけではないのでスイマセン。

Rubyで正規表現を扱う時によく書くパターン(と思われる)コードは下のような感じになる。

pattern = /([a-z]+)/
str = gets.chomp
if str =~ pattern
  puts $1
else
  puts "not match"
end

標準入力から読み込んだ文字列から、小文字のアルファベットが続く部分を抜き出す。
この時に使ってる$1とかが今回の説明する部分になる。

これってPerlライクな書き方になってるけど、そういう書き方は嫌じゃ!!って言う人はちゃんと別の書き方が用意されてる。

pattern = Regexp.compile("([a-z]+)")
str = gets.chomp
if match = pattern.match(str)
  puts match[0]
else
  puts "not match"
end

Regexpのcompileで正規表現オブジェクトを作って、それのmatchメソッドで正規表現にマッチした部分の情報を持つMatchDataクラスのオブジェクトが取得できる。(マッチする部分が無かったらnil)
これの[]演算子を使って$1とかに相当するものが取得できる。

$1,$2,$3って何だ?って思いながら使ってたけど、これって$~[0],$~[1],$~[2]と同じ意味らしい。
$~って何だ?ってのは、正規表現のmatchを呼び出したときにMatchDataをこっそり入れてるみたい。

自分のコードから$~ = matchみたいに設定すると$1 $2とかで取得できるものを任意のMatchDataに置きかえれる。
普通はしないだろうけど…

投稿日時 : 2008年3月9日 0:08

Feedback

# kiQjQNJHru 2011/12/13 17:30 http://www.birthcontrolremedy.com/birth-control/ya

Strange but true. Your resource is expensive. At least it could be sold for good money on its auction!...

# ugg sale 2012/10/19 15:24 http://www.superbootonline.com

Regards for helping out, wonderful info. "I have witnessed the softening of the hardest of hearts by a simple smile." by Goldie Hawn.

# cheap supra shoes 2012/12/08 9:08 http://supratkstore.webs.com/

I believe this website has some rattling excellent information for everyone. "As we grow oldthe beauty steals inward." by Ralph Waldo Emerson.

# longchamp 2012/12/14 22:53 http://www.saclongchampachete.info/category/longch

We found a multitude of great DVDs that any of us were excited to enjoy again. Over the course of two months.

# sac pliage longchamp pas cher 2012/12/15 15:56 http://www.sacslongchamp2012.info/sacs-longchamp-c

Very helpful info. Hope to view more items soon!

# sac longchamp acheter 2012/12/16 21:47 http://www.saclongchampachete.com

Thus, our shelves lead to filled with stuffs that we benefit from.

# longchamps 2012/12/17 7:55 http://www.longchampfr.info/category/sac-longchamp

The stars with this pool are the comments and also pictures tend to be secondary.

# burberry scarf sale 2012/12/17 21:05 http://www.burberryuksale.org/category/burberry-sa

We found a great number of great DVDs that we all were excited to view again. Over the length of a couple of months.

# sacs michael kors 2012/12/18 5:54 http://sac2012femmes.wordpress.com

Nobody could see someone to mug you in the basement.

# sneakers isabel marant 2012/12/18 20:29 http://isabelmarantbottes.webnode.fr

This is a really beneficial site publish, im delighted I discovered it.

# sacs longchamp 2012/12/18 20:59 http://www.longchampfr.info/category/sac-longchamp

Think it like a journal or possibly a magazine of general interest for anyone to look over. Funny, witty, controversial, entertaining, useful, acerbic, thoughtful, serious, curious, unexpected comebacks are actually welcome.

# burberryukoutlets.wordpress.com 2012/12/18 22:14 http://burberryukoutlets.wordpress.com

we re-watched the lord of any Rings trilogy, the Godfather trilogy, and concerning twenty various movies that many of us loved along with hadn¡¯t watched within the while.

# michael kors outlet 2012/12/19 13:49 http://sac-michael-kors.webnode.fr/blog/

The fashion don't flip flat plus Philips doesn't give you a travel pouch from the package.

# burberry scarf 2012/12/21 5:27 http://burberryoutlet2013sale.webeden.co.uk/#/abou

Its option are which means fantastic in addition to working fashion so speedy.

# destockchine index 2013/01/11 13:14 http://www.destockchinefr.fr/

Never male or female will your own holes, while the person who may be won‘capital t turn you into call.
destockchine index http://www.destockchinefr.fr/

# www.K77.fr 2013/03/03 14:48 http://www.k77.fr/

Throughout money our personal family realize everyone; for trouble young children and can our personal family. www.K77.fr http://www.k77.fr/

# casquette supreme 2013/03/04 23:26 http://www.b66.fr/

You shouldn't discuss about it your pleasure to a single much less fortunate enough more than oneself. casquette supreme http://www.b66.fr/

# spartoo 2013/03/04 23:27 http://www.c88.fr/

A colleague which you just buy with the brings might be purchased from your family. spartoo http://www.c88.fr/

# Air Jordan Retro 4 2013/03/05 0:46 http://www.jordanretro4air.com/

Nope woman or man may be valued at your favorite cry, therefore the person who is without a doubt garnered‘testosterone levels get you to be sad. Air Jordan Retro 4 http://www.jordanretro4air.com/

# casquette obey 2013/03/15 5:19 http://www.b44.fr/

No individual is going to be your favorite crying, and then the a person who is simply been successfull‘r allow you to be rallying cry. casquette obey http://www.b44.fr/

# pari street 2013/03/15 9:09 http://www.a88.fr/

Pleasure is definitely a aromatise it's hard to decant within a number of people while not ending up with a a small number of lowers within your family. pari street http://www.a88.fr/

# destock mode 2013/03/16 8:57 http://www.b77.fr/

An actual pal is probably not be anyone, although anyone will always be some sort of pal. destock mode http://www.b77.fr/

# destockchine 2013/03/24 0:44 http://d77.fr/

Where may perhaps be relationship devoid of take pleasure in, you'll find take pleasure in devoid of relationship. destockchine http://d77.fr/

# usine23 2013/03/24 0:44 http://e55.fr/

Located in successfulness some of our classmates and friends learn everyone; throughout adversity young children and can some of our classmates and friends. usine23 http://e55.fr/

# destockchine 2013/03/24 0:44 http://c99.fr/

Seriously like is simply fragile by begin, even so grows a lot more powerful as we grow older when it's sufficiently provided with. destockchine http://c99.fr/

# 太阳城 2013/04/03 7:35 http://tt6262.com/

Where there is certainly relationship not having adoration, you will encounter adoration not having relationship. 太?城 http://tt6262.com/

# gemo 2013/04/07 9:08 http://ruezee.com/

Relationship could possibly be the golden thread the fact neckties any paper hearts epidermis world. gemo http://ruezee.com/

タイトル
名前
Url
コメント