Need to match words from setence

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

Can anybodyhelp

···

--
Posted via http://www.ruby-forum.com/.

sentence = "this is important information"

if sentence.include? 'important'
  # do something here
end

You could use a regex too:

if sentence =~ /important/

Within your if code blocks, you can check which
combinations to use or avoid.

  sentence = "this is &@important,^% information"
  this should not match

  if sentence.include? '^'
    # Dont match here
  end

This is really very basic if / else conditionals.

Look at an easy ruby tutorial if this confuses you,
it is really the basic of any programming language
how to use if / else.

http://pine.fm/LearnToProgram/?Chapter=00

Work through this book at least once, then you know
how to use if / else and .include?.

If you need a regex:

  http://rubular.com/

But also read a tutorial what the meaning is of a
regex.

You can probably make a one liner regex that matches
to impotant, but not if it includes a "^"

···

--
Posted via http://www.ruby-forum.com/.

Hi,

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.

···

--
Posted via http://www.ruby-forum.com/.

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

Can anybodyhelp

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/.

What would make your requirements clear would be to write and post
some unit tests that demonstrate your success/fail conditions.

That might actually lead you to a solution, for that matter :slight_smile:

FWIW,

···

On Tue, Apr 10, 2012 at 10:47 AM, Lucky Nl <lists@ruby-forum.com> wrote:

Hope this clear now

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

Two things amaze me still- The use cases for regex, and the programmers who
still don't know how to use them :slight_smile:

(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.

--
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

Please see below cases hope you can understand now

my search word is "important"

am trying check this word existed in querystring or not

if querystring = “I am looking for important message” expected true

if querystring = “I am looking for @important. message” expected true

if querystring = “I am looking for @important message” expected true

if querystring = “I am looking for important. message” expected true

if querystring = “I am looking for importantdata message” expected
false

if querystring = “I am looking forimportant. message” expected false

if querystring = “I am looking @@important%. message” expected false

Hassan Schroeder wrote in post #1055884:

···

On Tue, Apr 10, 2012 at 10:47 AM, Lucky Nl <lists@ruby-forum.com> wrote:

Hope this clear now

What would make your requirements clear would be to write and post
some unit tests that demonstrate your success/fail conditions.

That might actually lead you to a solution, for that matter :slight_smile:

FWIW,

--
Posted via http://www.ruby-forum.com/\.