diff --git a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java index 30616e3..97b8d00 100644 --- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java @@ -19,7 +19,17 @@ public class DotenvParser { private static final Pattern WHITE_SPACE_REGEX = Pattern.compile("^\\s*$"); // ^\s*${'$'} - private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$"); // ^\s*([\w.\-]+)\s*(=)\s*([^#]*)?\s*(#.*)?$ + + // The follow regex matches key values. + // It supports quoted values surrounded by single or double quotes + // - Single quotes: ['][^']*['] + // The above regex snippet matches a value wrapped in single quotes. + // The regex snippet does not match internal single quotes. This is present to allow the trailing comment to include single quotes + // - Double quotes: same logic as single quotes + // It ignore trailing comments + // - Trailing comment: \s*(#.*)?$ + // The above snippet ignore spaces, the captures the # and the trailing comment + private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*(['][^']*[']|[\"][^\"]*[\"]|[^#]*)?\\s*(#.*)?$"); //"^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$"); // ^\s*([\w.\-]+)\s*(=)\s*([^#]*)?\s*(#.*)?$ private final DotenvReader reader; private final boolean throwIfMissing; diff --git a/src/test/java/tests/BasicTests.java b/src/test/java/tests/BasicTests.java index c15cfd7..502e6ed 100644 --- a/src/test/java/tests/BasicTests.java +++ b/src/test/java/tests/BasicTests.java @@ -16,6 +16,7 @@ public class BasicTests { put("WITHOUT_VALUE", ""); put("MULTI_LINE", "hello\\nworld"); put("TRAILING_COMMENT", "value"); + put("QUOTED_VALUE", "iH4>hb_d0#_GN8d]6"); }}; @Test(expected = DotenvException.class) diff --git a/src/test/resources/.env b/src/test/resources/.env index 2c940e0..d0ccf01 100644 --- a/src/test/resources/.env +++ b/src/test/resources/.env @@ -4,6 +4,7 @@ MY_TEST_EV2=my test ev 2 WITHOUT_VALUE= MULTI_LINE=hello\nworld TRAILING_COMMENT=value # comment +QUOTED_VALUE="iH4>hb_d0#_GN8d]6" # comment "test" ## Malformed EV! MY_TEST_EV3 diff --git a/src/test/resources/env b/src/test/resources/env index 8aaf95f..d01afb9 100644 --- a/src/test/resources/env +++ b/src/test/resources/env @@ -4,6 +4,7 @@ MY_TEST_EV2=my test ev 2 WITHOUT_VALUE= MULTI_LINE=hello\nworld TRAILING_COMMENT=value # comment +QUOTED_VALUE="iH4>hb_d0#_GN8d]6" # comment "test" ## Malformed EV! MY_TEST_EV3