# Anyway to make grep take a regular string?

**URL:** <https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361>\
**Category:** Ruby\
**Created:** [November 16, 2007, 7:49am UTC](https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361 "2007-11-16T07:49:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 7:49am UTC](https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361/1 "2007-11-16T07:49:27Z")

</div>

I know grep needs to take an regular expression.

Is there anyway to have it take a regular string instead? without using  
regular expressions?

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 8:16am UTC](https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361/2 "2007-11-16T08:16:12Z")

</div>

On 16 Nov 2007, at 15:49, Feng T. wrote:

> I know grep needs to take an regular expression.
> 
> ## Is there anyway to have it take a regular string instead? without using regular expressions?

What grep are you referring to? Enumerable#grep doesn’t need an RE. A  
String works fine as does any === implementing object:

## -------------------------------------------------------- Enumerable#grep enum.grep(pattern) =\> array enum.grep(pattern) {| obj | block } =\> array

```
  Returns an array of every element in _enum_ for which +Pattern ===
  element+. If the optional _block_ is supplied, each matching
  element is passed to it, and the block's result is stored in the
  output array.

     (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
     c = IO.constants
     c.grep(/SEEK/) #=> ["SEEK_END", "SEEK_SET", "SEEK_CUR"]
     res = c.grep(/SEEK/) {|v| IO.const_get(v) }
     res #=> [2, 0, 1]

```

[alexg@powerbook]/Users/alexg/Desktop(7): irb  
irb(main):001:0\> [‘a’,‘b’,‘c’].grep(‘a’)  
=\> [“a”]

Or do you mean you want use a String as if it were an RE. If so then  
you can just make one from the String:

irb(main):003:0\> [‘foo’,‘bar’,‘foobar’].grep(Regexp.new(‘foo’))  
=\> [“foo”, “foobar”]

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 9:07am UTC](https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361/3 "2007-11-16T09:07:31Z")

</div>

> Alex G.

Yah, I’m use the Enumerable#grep

Here’s what I’m trying to do

def literal (search\_term, \*inputs)  
inputs.each do |to\_search|  
puts open(to\_search).grep(search\_term)  
end  
end

When I run that method, it just gives me an blank screen, while using  
Regexep(search\_term), i get results.

How do I get an exact match?

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 9:32am UTC](https://www.ruby-forum.com/t/anyway-to-make-grep-take-a-regular-string/120361/4 "2007-11-16T09:32:53Z")

</div>

On 16 Nov 2007, at 17:07, Feng T. wrote:

> end  
> end
> 
> When I run that method, it just gives me an blank screen, while using  
> Regexep(search\_term), i get results.
> 
> How do I get an exact match?

Perhaps there’s a line ending “\n” you’re missing. Your method works  
for me below if I remember the line endings:

[alexg@powerbook]/Users/alexg/Desktop(8): echo ‘foo’ \> foo.txt  
[alexg@powerbook]/Users/alexg/Desktop(9): echo ‘bar’ \> bar.txt  
[alexg@powerbook]/Users/alexg/Desktop(10): echo ‘foobar’ \> foobar.txt  
[alexg@powerbook]/Users/alexg/Desktop(11): irb  
irb(main):001:0\> files = [‘foo.txt’,‘bar.txt’,‘foobar.txt’]  
=\> [“foo.txt”, “bar.txt”, “foobar.txt”]  
irb(main):002:0\> def literal(search\_term, _inputs)  
irb(main):003:1\> inputs.each do |to\_search|  
irb(main):004:2_ puts open(to\_search).grep(search\_term)  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> literal(‘foo’,\*files)  
=\> [“foo.txt”, “bar.txt”, “foobar.txt”]  
irb(main):008:0\> literal(Regexp.new(‘foo’),\*files)  
foo  
foobar  
=\> [“foo.txt”, “bar.txt”, “foobar.txt”]  
irb(main):009:0\> literal(“foo\n”,\*files)  
foo  
=\> [“foo.txt”, “bar.txt”, “foobar.txt”]

Alex G.

Bioinformatics Center  
Kyoto University
