Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I put up an intranet site that loops through a .csv dump of our customer database and uses a form to help look up account numbers.
I want to treat all of my keywords as wild card terms, but respect their order. For example, if I have company A: "The Monogramme Shoppe" and company B: "Monograms & More at The Shop", I want to return A and B options if I type "mono shop" in the form field. This code does that:
company_lookup = company_lookup.split(" ")
counter = company_lookup.length
company_lookup.each do |com|
if company.downcase.include? com.downcase
counter = counter - 1
end
end
if counter == 0
match_found = true
account_number = row[2].to_s
matches.push [account_number, company]
end
But if I type "mono the", I also get both results. There, I only want to get the B result.
Is there any way to use regular expressions to, say look for PartialKeyword1 and PartialKeyword2 in a string and return true if matched?
You can use the following code to construct a regular expression to match the company name, and then use this regular expression to find the matched record.
company_lookup = company_lookup.split(" ").map{|r| Regexp.quote(r)}.join('.*?')
if company =~ /#{company_lookup}/i
matches.push [row[2].to_s, company]
end
If performance is a big concern, or the data size is huge, you'd better try some full text search engine, such as Thinking Sphinx
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need a regular expression that only matches three digit numbers in the following array. I need the result to be a new array.
Input:
my_array = [111,45456,456,74897,787,45466,789,6587,784,234,456,4658,4587,235,456]
Desired output:
new_array = [111,456,787,789,784,234,456,235,456]
Why regular expression on numbers? You can select all numbers less than 1000 and greater than 99.
my_array.select { |n| n<1000 && n>99 }
Just the regexp would look like this: /^\d{3}$/. But if you'd like an expression that would return an array of values that match that expression this would do it: my_array.select{ |num| num.to_s.match(/^\d{3}$/) }.
Take a look at RegExr to learn more about Regular Expressions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a quick search rails app feature. Any help with how to structure the following matching specifications would be appreciated.
Basically, when a user enters a name to search, they should get results based on the following matching criteria.
accept match on first 3 chars (e.g. Jon for Jones)
reject match on less than 3 chars (e.g. Jo for Jones)
accept exact match for 2 char author name (e.g. Li for Li)
reject exact match on 1 char author name
reject mismatch on chars beyond 3 (e.g. reject Jonis for Jones)
Can this be done with a regular expression?
matchto = 'Jones'.downcase
input = 'Jon'.downcase
matchto.start_with?(input) && 1 < input.length &&
( input.length == matchto.length || 2 < input.length )
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a text file with contents as:
...
*..
*..
...
The vehicle used are:
*Car(Marathi, Nissan, Toyota)..
*Bikes(Yamaha, Hero Honda)
*...
*...
*...so on..
The items used are...
*..
*..
Now I need to search for the key word "vehicle" and put the options Car(Marathi, Nissan, Toyota)..), Bikes(Yamaha, hero honda), etc into a list.
i.e. Everything that comes after "*" in a line, must be an item of that list.
Linq must be used or any other means where loops are not allowed.
The desired result is not really clear.
If you need a List<string>, containing
"Car(Marathi, Nissan, Toyota).."
"Bikes(Yamaha, Hero Honda)"
"..."
"..."
"...so on..
you could do
var result = File.ReadAllLines(#"<pathToYourFile>")
//skip lines without "vehicle"
.SkipWhile(m => !m.Contains("vehicle"))
//skip the line with "vehicle"
.Skip(1)
//take the following lines starting with an "*"
.TakeWhile(m => m.StartsWith("*"))
//remove the "*"
.Select(m => m.Replace("*", string.Empty))
//enumerate to get a List<string>
.ToList();