What would be the regex for this pattern?

Tuesday, July 1, 2014

My Java program, in certain point, receives a string containing a couple of key-value properties like this example:



param1=value Param2=values can have spaces PARAM3=values cant have equal characters


The parameters' name/key are composed by a single word (a-z, A-Z, _ and 0-9) and are followed by an = character (not separated by spaces) and it's value. The value is a text that can contain spaces and last until the end of the string or the begin of another parameter. (which is a word followed by equals and it's value, etc.)


I need to extract a Properties object (string-to-string map) from this string. I was trying to use regex to find each key-value set. The code is like this:



public static String createProperties(String str) {
Properties prop = new Properties();
Matcher matcher = Pattern.compile(some regex).match(str);

while (matcher.find()) {
String match = matcher.group();
String param = ...; // What comes before '='
String value = ...; // What comes after '='
prop.setProperty(param, value);
}

return prop;
}


But the regex wrote is not working correctly.



String regex = "(\\w+=.*)+";


Since .* tells the regex to get "anything" it found, it will match the entire string. I want to tell the regex to search until it finds another \\w=.*. (word followed by equals and something after)


How could I write this regex? Or what would be another solution for the problem using regex?







http://ift.tt/1xehBUD