From: Peter Date: 2012-04-11T23:23:09+09:00 Subject: Re: need to match words from setence --f46d0408d6735b4c4804bd67fac8 Content-Type: text/plain; charset=ISO-8859-1 Two things amaze me still- The use cases for regex, and the programmers who still don't know how to use them :) (also, hello! New on the list and hope to pop in and contribute now and then. Been using Ruby for 5 years now, about time I get on this list. I do frequent #ruby on irc.freenode.net as "lectrick"...) On Wed, Apr 11, 2012 at 7:21 AM, Jan E. wrote: > You still haven't said what you actually want to do (and some examples > don't really help), so this is only a guess: > > search_word = 'important' > pattern = / > # beginning of the string or position after whitespace > (?:(?<=\s)|\A) > # optional "special character": > # any printable ASCII character other than [a-zA-z0-9] > (?[\p{ASCII}&&\p{Graph}&&\p{^Alnum}])? > # the actual search word (don't forget to escape!) > #{Regexp.escape search_word} > # optional "special character" > \g? > # end of the string or position before whitespace > (?:(?=\s)|\z) > /x > > [ > 'I am looking for important message', > 'I am looking for @important. message', > 'I am looking for @important message', > 'I am looking for important. message', > 'I am looking for importantdata message', > 'I am looking forimportant. message', > 'I am looking @@important%. message' > ].each do |test_string| > puts "#{test_string}: #{not test_string.match(pattern).nil?}" > end > > But again: This is only a guess. The pattern may not work in special > cases, and there may be much better solutions (without the obscure > special chars). But we won't find out unless you tell us what this whole > thing is for. > > -- > Posted via http://www.ruby-forum.com/. > > -- "I'd put my money on the sun and solar energy. What a source of power! I hope we don't have to wait until oil and coal run out before we tackle that." -Thomas Edison, 1931 --f46d0408d6735b4c4804bd67fac8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Two things amaze me still- The use cases for regex, and the programmers who= still don't know how to use them :)

(also, hello! New on the li= st and hope to pop in and contribute now and then. Been using Ruby for 5 ye= ars now, about time I get on this list. I do frequent #ruby on irc.freenode.net as "lectrick"...)
On Wed, Apr 11, 2012 at 7:21 AM, Jan E. <lists@ruby-forum.= com> wrote:
You still haven't said what you actually want to do (and some examples<= br> don't really help), so this is only a guess:

search_word =3D 'important'
pattern =3D /
=A0# beginning of the string or position after whitespace
=A0(?:(?<=3D\s)|\A)
=A0# optional "special character":
=A0# any printable ASCII character other than [a-zA-z0-9]
=A0(?<special_char>[\p{ASCII}&&\p{Graph}&&\p{^Alnum}= ])?
=A0# the actual search word (don't forget to escape!)
=A0#{Regexp.escape search_word}
=A0# optional "special character"
=A0\g<special_char>?
=A0# end of the string or position before whitespace
=A0(?:(?=3D\s)|\z)
/x

[
=A0'I am looking for important message',
=A0'I am looking for @important. message',
=A0'I am looking for @important message',
=A0'I am looking for important. message',
=A0'I am looking for importantdata message',
=A0'I am looking forimportant. message',
=A0'I am looking @@important%. message'
].each do |test_string|
=A0puts "#{test_string}: #{not test_string.match(pattern).nil?}"=
end

But again: This is only a guess. The pattern may not work in special
cases, and there may be much better solutions (without the obscure
special chars). But we won't find out unless you tell us what this whol= e
thing is for.



--
= "I'd put my money on the sun and solar energy. What a source of po= wer! I hope we don't have to wait until oil and coal run out before we = tackle that."
-Thomas Edison, 1931
--f46d0408d6735b4c4804bd67fac8--