Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements support for quoted values #46

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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