Skip to content

Commit

Permalink
Implements support for quoted values (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio authored Jan 16, 2023
1 parent c93e7e8 commit bbfbcfa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/tests/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/env
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bbfbcfa

Please sign in to comment.