ruby - matching single letters in a sentence with a regular expression -
i want match single letters in sentence. in ...
i want have turkey. may i. 20,000-t bar-b-q
i'd match
*i* want have turkey. may *i*. *i* 20,000-t bar-b-q
right i'm using
/\b\w\b/
as regular expression, matching
*i* want have turkey. may *i*. *i* 20,000-*t* bar-*b*-*q*
any suggestions on how past last mile?
use negative lookbehind , negative lookahead fail if previous character word or hyphen, or if next character word or hyphen:
/(?<![\w\-])\w(?![\w\-])/
example: http://www.rubular.com/r/9upmgfg9u4
note mentioned rtcherry, match single numbers. prevent may want change \w
outside of character classes [a-za-z]
.
Comments
Post a Comment