Skip to content

Commit bbfbcfa

Browse files
authoredJan 16, 2023
Implements support for quoted values (#46)
1 parent c93e7e8 commit bbfbcfa

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed
 

‎src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
public class DotenvParser {
2020

2121
private static final Pattern WHITE_SPACE_REGEX = Pattern.compile("^\\s*$"); // ^\s*${'$'}
22-
private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$"); // ^\s*([\w.\-]+)\s*(=)\s*([^#]*)?\s*(#.*)?$
22+
23+
// The follow regex matches key values.
24+
// It supports quoted values surrounded by single or double quotes
25+
// - Single quotes: ['][^']*[']
26+
// The above regex snippet matches a value wrapped in single quotes.
27+
// The regex snippet does not match internal single quotes. This is present to allow the trailing comment to include single quotes
28+
// - Double quotes: same logic as single quotes
29+
// It ignore trailing comments
30+
// - Trailing comment: \s*(#.*)?$
31+
// The above snippet ignore spaces, the captures the # and the trailing comment
32+
private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*(['][^']*[']|[\"][^\"]*[\"]|[^#]*)?\\s*(#.*)?$"); //"^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$"); // ^\s*([\w.\-]+)\s*(=)\s*([^#]*)?\s*(#.*)?$
2333

2434
private final DotenvReader reader;
2535
private final boolean throwIfMissing;

‎src/test/java/tests/BasicTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class BasicTests {
1616
put("WITHOUT_VALUE", "");
1717
put("MULTI_LINE", "hello\\nworld");
1818
put("TRAILING_COMMENT", "value");
19+
put("QUOTED_VALUE", "iH4>hb_d0#_GN8d]6");
1920
}};
2021

2122
@Test(expected = DotenvException.class)

‎src/test/resources/.env

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ MY_TEST_EV2=my test ev 2
44
WITHOUT_VALUE=
55
MULTI_LINE=hello\nworld
66
TRAILING_COMMENT=value # comment
7+
QUOTED_VALUE="iH4>hb_d0#_GN8d]6" # comment "test"
78

89
## Malformed EV!
910
MY_TEST_EV3

‎src/test/resources/env

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ MY_TEST_EV2=my test ev 2
44
WITHOUT_VALUE=
55
MULTI_LINE=hello\nworld
66
TRAILING_COMMENT=value # comment
7+
QUOTED_VALUE="iH4>hb_d0#_GN8d]6" # comment "test"
78

89
## Malformed EV!
910
MY_TEST_EV3

0 commit comments

Comments
 (0)
Please sign in to comment.