This is the third time now you post your problem (in different
variations). Rather than trying again and again to find an answer to
this detail, you maybe should tell us *WHAT* you want to do.
What are you working on? What is this word search for?
It's much easier to find a solution if you know the whole purpose and
not only have tiny (and rather vague) information about some details.
And maybe we even find a better solutions than yours. This "special
chars" thing sounds rather strange to me.
Hmm include method will match substring too.This is not i want .I want
to match exactword with wordboundary.
and additional scenario is only search word can have leading, tralining
specialchar that too only one occurence should allow
Hope this clear now
Lucky Nl wrote in post #1055770:
···
Hi friends,
I have small requirement
Example
sentence = "this is important information"
Now i want search word "important" from sentence.
I can do this match
Suppose if i have sentence like
case1:
sentence = "this is @important, information"
in this case i should get to match with word "important"
Case2:
sentence = "this is &@important,^% information"
in this case i should get to match with word "important"
this should not match
means word has only one traling/leading specialchar would be fine
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]
(?<special_char>[\p{ASCII}&&\p{Graph}&&\p{^Alnum}])?
# the actual search word (don't forget to escape!)
#{Regexp.escape search_word}
# optional "special character"
\g<special_char>?
# 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.
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. <lists@ruby-forum.com> 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]
(?<special_char>[\p{ASCII}&&\p{Graph}&&\p{^Alnum}])?
# the actual search word (don't forget to escape!)
#{Regexp.escape search_word}
# optional "special character"
\g<special_char>?
# 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.
--
"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