diff --git a/lib/dotenv/environment.rb b/lib/dotenv/environment.rb
index <HASH>..<HASH> 100644
--- a/lib/dotenv/environment.rb
+++ b/lib/dotenv/environment.rb
@@ -14,7 +14,7 @@ module Dotenv
         |               #   or
         [^#\n]+         #   unquoted value
       )                 # value end
-      (?:\#.*)?         # optional comment
+      (?:\s*\#.*)?      # optional comment
       \z
     /x
 
diff --git a/spec/dotenv/environment_spec.rb b/spec/dotenv/environment_spec.rb
index <HASH>..<HASH> 100644
--- a/spec/dotenv/environment_spec.rb
+++ b/spec/dotenv/environment_spec.rb
@@ -82,6 +82,10 @@ describe Dotenv::Environment do
     expect(env("foo=bar # this is foo")).to eql('foo' => 'bar')
   end
 
+  it 'allows # in quoted value' do
+    expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')
+  end
+
   it 'ignores comment lines' do
     expect(env("\n\n\n # HERE GOES FOO \nfoo=bar")).to eql('foo' => 'bar')
   end
test case for # in a quoted value