JamesAbellana
JamesAbellana

Reputation: 1

See if the beginning of a line matches a regex character

There are lines inside a file that contain !. I need all other lines. I only want to print lines within the file that do not start with an exclamation mark.

The line of code which I have written so far is:

unless parts.each_line.split("\n" =~ /^!/)
  # other bit of nested code
end

But it doesn't work. How do I do it?

Upvotes: 0

Views: 168

Answers (2)

the Tin Man
the Tin Man

Reputation: 160551

As a start I'd use:

File.foreach('foo.txt') do |li|
  next if li[0] == '!'
  puts li
end

foreach is extremely fast and allows your code to handle any size file - "scalable" is the term. See "Why is "slurping" a file not a good practice?" for more information.

li[0] is a common idiom in Ruby to get the first character of a string. Again, it's very fast and is my favorite way to get there, however consider these tests:

require 'fruity'

STR = '!' + ('a'..'z').to_a.join # => "!abcdefghijklmnopqrstuvwxyz"

compare do
  _slice { STR[0] == '!' }
  _start_with { STR.start_with?('!') }
  _regex { !!STR[/^!/] }
end

# >> Running each test 32768 times. Test will take about 2 seconds.
# >> _start_with is faster than _slice by 2x ± 1.0
# >> _slice is similar to _regex

Using start_with? (or its String end equivalent end_with?) is twice as fast and it looks like I'll be using start_with? and end_with? from now on.

Combine that with foreach and your code will have a decent chance of being fast and efficient.

See "What is the fastest way to compare the start or end of a String with a sub-string using Ruby?" for more information.

Upvotes: 2

Richard Hamilton
Richard Hamilton

Reputation: 26444

You can use string#start_with to find the lines that start with a particular string.

file = File.open('file.txt').read
file.each_line do |line|
  unless line.start_with?('!')
    print line
  end
end

You can also check the index of the first character

unless line[0] === "!"

You can also do this with Regex

unless line.scan(/^!/).length

Upvotes: 0

Related Questions