开发者

parsing a string by regular expression

开发者 https://www.devze.com 2023-03-19 16:37 出处:网络
I have a string of \"name\"=>\"3B Ae\", \"note\"=>\"Test fddd \\\"33 Ae\\\" FIXME\", \"is_开发者_StackOverflowon\"=>\"keke, baba\"

I have a string of

"name"=>"3B Ae", "note"=>"Test fddd \"33 Ae\" FIXME", "is_开发者_StackOverflowon"=>"keke, baba"

and i want to parse it by a java program into segments of

name
3B Ae
note
Test fddd \"33 Ae\" FIXME
is_on
keke, baba

It is noted that the contents of the string, i.e. name, 3B Ae, are not fixed. Any suggestion?


If you:

  • replace => with :
  • Wrap the full string with {}

The result will look like this, which is valid JSON. You can then use a JSON parser (GSON or Jackson, for example) to parse those values into a java object.

{
    "name": "3B Ae",
    "note": "Test fddd \"33 Ae\" FIXME",
    "is_on": "keke, baba"
}

If you have control over the process that produces this string, I highly recommend that you use a standard format like JSON or XML that can be parsed more easily on the other end.


Because of the quoting rules, I'm not certain that a regular expression (even a PCRE with negative lookbehinds) can parse this consistently. What you probably want is to use a pushdown automaton, or some other parser capable of handling a context-free language.


If you can make sure your data (key or value) does not have a => or a , (or find some other delimiters that will not occur), the solution is pretty simple:

  • Split the string by , you get the key => value pairs
  • Split the key value => pairs by => you get what you want

if inputString holds
"name"=>"3B Ae", "note"=>"Test fddd \"33 Ae\" FIXME", "is_on"=>"keke baba"
(from a file for instance)
(I have changed the , to ; from between keke and baba)

    String[] keyValuePairs = inputString.split(",");
    for(String oneKeyValue : keyValuePairs)
    {
       String[] keyAndValue = oneKeyValue.split("=>");
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号