-1

What is the regex to parse a String into a java.util.Map where the String have a defined format like this:

message_type={any_text}&message_number={digits}&code={digits}&id={digits}&message={any_text}&timestamp={digits_with_decimal}

The goal is to generate a Map with message_type, message_number, code, id, message and timestamp keys.

Is this something that a regex can parse? Or this would need a parser with grammar?

Update:

This is not the same URI parsing problem. The message here is a body String thus may include '&' in the message part. Using the same split with '&' might cause a wrong split.

Also, the { } are just the ideal function that needs to be replaced by the regex.

quarks
  • 33,478
  • 73
  • 290
  • 513
  • 2
    Can't you use a standard library for parsing query strings? – Andy Turner Mar 14 '15 at 17:10
  • 1
    See http://stackoverflow.com/questions/13592236/parse-the-uri-string-into-name-value-collection-in-java – Andy Turner Mar 14 '15 at 17:11
  • My concern with splitting is that its possible that the message part may contain the character '&' causing it to split there also which is not a desired output – quarks Mar 14 '15 at 17:14
  • You should probably write more about data you want to parse. Can there be line breaks? Can input contain more than one `message_type` element? – Pshemo Mar 14 '15 at 17:19
  • The format is defined and there is no duplicate key – quarks Mar 14 '15 at 17:27
  • Are you assuming that data will be always in good format or do you want to test if `message_number={digits}` really contains only digits? – Pshemo Mar 14 '15 at 17:28
  • `String[] parts = command.substring(0, command.length() - 1).split("}&");` Then split parts using `"={"`. – Bubletan Mar 14 '15 at 17:30
  • @Bubletan As stated earlier `&` can be part of data, also something tells me that `{` and `}` are not required and they exist in `{any_text}` just to describe expected data-type. – Pshemo Mar 14 '15 at 17:35
  • Have you tried using [groups](http://www.regular-expressions.info/brackets.html)? – Pshemo Mar 14 '15 at 17:42

1 Answers1

1

I think the key is to use lazy evaluation and the following should do the trick:

([a-zA-Z].*?={.*?})

Start with a letter (lower or upper) till the first occurrence of "=" and then get the value including the braces and repeat.

Demo: https://regex101.com/r/iE0hU3/1

Khanna111
  • 3,627
  • 1
  • 23
  • 25