开发者

Java Regular Expression to search in String

开发者 https://www.devze.com 2023-01-28 01:17 出处:网络
How 开发者_开发百科will you proceed the following string: [p(\'WHITE\'),p(\'GREEN\'),p(\'GREEN\',\'RED\'),p(\'GREEN\',\'YELLOW\'),p(\'GREEN\',\'YELLOW\',\'RED\')]

How 开发者_开发百科will you proceed the following string:

[p('WHITE'),p('GREEN'),p('GREEN','RED'),p('GREEN','YELLOW'),p('GREEN','YELLOW','RED')]

to get a List of Nodes as suggested in the answers.


It depends on how complex the expressions can get. I would definitely write a proper parser for it, since only then can I be confident to have done it properly. Plus, I will parse the string into an abstract syntax tree so that at the end I will automatically have a nicely modeled tree of objects:

class Node { ... }

class StringLiteral extends Node {
  public String getValue() { ... }
}

class Term extends Node {
  public String getName() { ... }
  public int getArity() { ... }
  public Node getArgument(int index) { ... }
}
0

精彩评论

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