Search

Hi,

I need to implment the search using Ruby . The search word is more than one
means my program getting problem.
How can I solve that.
For example ,

    I am having one file . In that file more than 100 lines are there.
I have to search some three words . The first two words there in first line
and the another one word is there in
another one line means How can I match that 3 words.

Please any one help me..

Geetha.

It was very difficult to parse your question. I think what you meant is this:

'Given a multi-line string, how do I search for a sequence of words
that spans multiple lines?'

E.g. for the string

  haystack = "blah blah blah string\nto find blah blah blah"

a regular expression to match this might be

/\bstring\s+to\s+find\b/

Given a search string that's just a sequence of words

  search_string = "string to find"

we can construct a regular expression to match it:

  regexp = Regexp.new("\\b" + search_string.split.map{ |word|
Regexp.escape(word) }.join("\\s+") + "\\b")
  # => /\bstring\s+to\s+find\b/

and search using normal methods:

  haystack =~ regexp # => 15

Of course, for a real text search, you'll probably want to do some
stemming and normalisation, but I don't think that's what you were
asking about.

Does that help?
Paul.

···

On 19/06/07, geetha <sangeetha.geethu05@gmail.com> wrote:

    I am having one file . In that file more than 100 lines are there.
I have to search some three words . The first two words there in first line
and the another one word is there in
another one line means How can I match that 3 words.

    I am having one file . In that file more than 100 lines are there.
I have to search some three words . The first two words there in first
line
and the another one word is there in
another one line means How can I match that 3 words.

Why don't you try ruby regexp?

···

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

I tried your concept. But it wont escape the special character what the
regexp return.
For example ,

"widely implemented" is my search string , the regular expression return
like
(?-mix:widely\\\s+ ).

So while the time of matching it had a problem.
So I am working on that.

···

On 6/20/07, Noorul Islam K M <gnukid@gmail.com> wrote:

> I am having one file . In that file more than 100 lines are there.
> I have to search some three words . The first two words there in first
> line
> and the another one word is there in
> another one line means How can I match that 3 words.

Why don't you try ruby regexp?

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