Geo
Geo

Reputation: 96767

Why is this regex not working properly?

I have this regex: ^\/\* to check and see if a file contains those two characters at the beginning. I'm iterating over many c++ source files trying to see which of them contain that. The problem is, that if a file contains this:

#include <source.h>

/* this is a comment */

this also matches the regex. I don't understand why, as the regex doesn't have the multiline flag on.

Here's the code for the regex:

multi = /^\/\*/

Why isn't this matching only at the beginning of the text? Here's basically everything I'm doing:

data = File.read(filename)
if data =~ multi
   puts "file starts with multiline header"
end

Upvotes: 2

Views: 186

Answers (4)

Svante
Svante

Reputation: 51501

Why use a regular expression?

multi = "/*"
data = File.read(filename)
if data[0..2] == multi
   puts "file starts with multiline header"
end

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336098

Use \A (beginning of string) instead of ^ (beginning of line).

The interpretation of ^ is not completely consistent between flavors. Sometimes you need to set a mode modifier for multi-line strings, but not always. \A is consistent (although not available in all flavors, but most of them. Exceptions are XML, POSIX ERE/BREs and a few others).

Upvotes: 3

Pherrymason
Pherrymason

Reputation: 8067

I don't know of ruby internals, but try this:

/^[^a-zA-Z#<>]/*/

The first part ensures that any valid character is not found before your multiline comment. Please, note that [^a-zA-Z#<>] is just an example, you should complete it with a valid combination.

Upvotes: 0

Andomar
Andomar

Reputation: 238048

In Ruby ^ matches after every newline. Use \A to match only at the start of the entire string:

multi = /\A\/\*/

Upvotes: 5

Related Questions