I have this data
ReferenceDataLocation = as
##############################################################################
#
# LicenseKey
# Address Doctor License
#
##############################################################################
LicenseKey = al
which I’d like to capture only key value pairs eg: ReferenceDataLocation = as and LicenseKey = al
I wrote (?xms)(^[\w]+.*?)(?=^[\w]+|\z) regex which is perfect except the fact that it also captures ##### part, which is not key value pair.
Please help me modify the same regex (?xms)(^[\w]+.*?)(?=^[\w]+|\z) to only get ReferenceDataLocation = as and LicenseKey = al
Note: Here you can try out
Update
I tried (?xms)(^[\w]+.*?)(?=^[\w^#]+|\z) it works in the site but gives me an error in java
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 31
(?xms)(^[\w]+.*?)(?=^[\w^#]+|\Z)
^
Updat Regex that works for me
(?xms)(^[\w]+.*?)(?=^[\w^\s]+|\z)
You can’t do that with a simple regex-match. You can’t account for occurrences like these:
The regex engine cannot look behind from
LicenseKeyto the end of the line. This is not supported in Java’s regex engine (unbounded look-behinds).But what you posted looks like it’s just a properties file. Try this:
which will print:
Note that your input file needn’t be called
test.properties, you can give it any name you like.And if you don’t know the keys up front, you can simply iterate over all entries in your properties file like this:
which prints:
And there’s also
Properties#stringPropertyNames()which returns aSet<String>that represents all keys in the properties file (see the API docs for more info).See: