From: Chad Perrin Date: 2011-03-18T15:31:11+09:00 Subject: matching a word in any number of characters --SLDf9lqlvOQaIe6s Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I have need of some code to match any of a number of words in any number of characters from the beginning of the word between the smallest number of characters that will uniquely identify it and the total number of characters in the word. My solution so far has been a set of kind of ugly, but conceptually simple, regular expressions: def expand_attribute_arg(attr) if attr.to_s.downcase.match( /^s[t]?[r]?[e]?[n]?[g]?[t]?[h]?$/ ) return 'Strength' elsif attr.to_s.downcase.match( /^d[e]?[x]?[t]?[e]?[r]?[i]?[t]?[y]?$/ ) return 'Dexterity' elsif attr.to_s.downcase.match( /^co[n]?[s]?[t]?[i]?[t]?[u]?[t]?[i]?[o]?[n]?$/ ) return 'Constitution' elsif attr.to_s.downcase.match( /^i[n]?[t]?[e]?[l]?[l]?[i]?[g]?[e]?[n]?[c]?[e]?$/ ) return 'Intelligence' elsif attr.to_s.downcase.match( /^w[i]?[s]?[d]?[o]?[m]?$/ ) return 'Wisdom' elsif attr.to_s.downcase.match( /^ch[a]?[r]?[i]?[s]?[m]?[a]?$/ ) return 'Charisma' else puts "#{attr} is an invalid attribute value." exit(1) end end The code works fine, but it just looks ugly and redundant to me, and I wonder if there's some simple, elegant approach that escapes me at the moment. Does anyone have any (fairly simple) ideas for how to tidy that up a bit? I keep staring at it and thinking "There should be a way to do this with an array of the terms to match that doesn't involve eval," but it's just not coming to me. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --SLDf9lqlvOQaIe6s Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk2C+W4ACgkQ9mn/Pj01uKWMGQCdEQwpLIOJTWHfNmba+4CfagW5 1DMAnioQlZNh4gHf0GIMPqSBNcEypUHs =k3j0 -----END PGP SIGNATURE----- --SLDf9lqlvOQaIe6s--