From: "Jesús Gabriel y Galán" Date: 2008-06-18T01:47:33+09:00 Subject: Re: Regular Expressions On Tue, Jun 17, 2008 at 6:08 PM, Justin To wrote: > Hello! I'm trying this problem that says I must match versions in a CSV > file, > > could be anything like: > > v.6.0.3-3 > aajd4-43_3 > ABCD 5.0 > ABCDv.5.0 > A 3.40 > ... > > With the other fields in mind, I thought "heck, looks like versions are > the only ones that contain a series of letters, digits, periods, > underscores and dashes..." > > I'm pretty new to Ruby so I don't have very much experience with regular > expressions. Is it possible to make just one regular expression to > fulfill my problem? I need a regular expression that will return: > > v.6.0.3-3: true because there's a v followed by a series of '.' and > digits > aajd4-43_3: true because there's a series of digits, '-', and '_' > ABCD 5.0: true because there's a series of digits and '.' > ABCDv.5.0: true... > A 3.40: true... I think there's some information missing here: how many of these characters form a "series"? More than 1? Do they have to be interleaved in some order, like, you need digits followed by a . a - or a _ followed by more digits, or it doesn't matter. The simplest case: two or more of those characters in a row: irb(main):023:0> versions = ["v.6.0.3-3", "aajd4-43_3","ABCD 5.0", "ABCDv.5.0", "A 3.40"] => ["v.6.0.3-3", "aajd4-43_3", "ABCD 5.0", "ABCDv.5.0", "A 3.40"] irb(main):024:0> r = /[.-_1-9]{2,}/ => /[.-_1-9]{2,}/ irb(main):025:0> versions.each {|x| puts "#{x}: #{(x =~ r) != nil}"} v.6.0.3-3: true aajd4-43_3: true ABCD 5.0: true ABCDv.5.0: true A 3.40: true 1 or more digits, followed by . or _ or -, followed by one or more digits: irb(main):030:0> r = /\d+[-._]\d+/ => /\d+[-._]\d+/ irb(main):031:0> versions.each {|x| puts "#{x}: #{(x =~ r) != nil}"} v.6.0.3-3: true aajd4-43_3: true ABCD 5.0: true ABCDv.5.0: true A 3.40: true You will have to refine your requirements a little bit, in order to choose among these (and any variations on this). Jesus.