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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -