0

Hello fellow programmers

i have a small problem and can't seem to find the right answere.

So my idea is i get a String with a predefined function from me. It will look something like this:

 String expression = "map(%set@IDENTIFIER%, get(%key@Longitude:Number%), get(%key@Latitude:Number%), map(%set@IDENTIFIER%, get(%key@Longitude:Number%), get(%key@Latitude:Number%)))";

It can be much more complicated or even simpler when i only use get for example.

What i want to do is to split every function in to its parameter this can also be functions or a normal String or Number.

What i thought is i can use regex to do this but i can't seem to find the right thing.

My regex so far:

String regex = "((?<method>map|get)\\((?<parameter>\\%(?<dynamic>set\\@[a-zA-Z_]+|\\$[0-9]+|key\\@[a-zA-Z_]+|set\\@[a-zA-Z_]+)(?<type>:[a-zA-Z]+)*\\%,*)+\\))";

What i do is

Pattern pa = Pattern.compile(regex);
Matcher m = pa.matcher(expression);
while (m.find()) {
   System.out.println("Method " + m.group("method"));
   System.out.println("Parameter " + m.group("parameter"));
   System.out.println("Type" + m.group("type"));
}

My Output is than this:
Method get
Parameter %key@Longitude:Number%
Type:Number
Method get
Parameter %key@Latitude:Number%
Type:Number
Method get
Parameter %key@Longitude:Number%
Type:Number
Method get
Parameter %key@Latitude:Number%
Type:Number

But what i miss is the function map with all its parameter.

Hope it is kind of understandable what i mean.

4
  • 1
    You miss it because all the methods are nested inside the maps. See demo. Java regex does not support recursion. Commented Jan 29, 2016 at 16:23
  • 3
    Probably regex is not your solution here. Commented Jan 29, 2016 at 16:23
  • This sounds more like a job for a recursive descent parser. Check out ANTLR for example if you do not want to write it yourself. Commented Jan 29, 2016 at 17:05
  • @Sorcen that looks interesting thanks Commented Jan 29, 2016 at 17:41

2 Answers 2

3

Try this ANTRL syntax.

grammar Method;

method      : ('map' | 'get') '(' element (',' element )* ')';
element     : method | parameter;
parameter   : '%' dynamic type* '%';
dynamic     : 'set' '@' ID | 'key' '@' ID | '$' NUMBER;
type        : ':' ID;

ID          : [a-zA-Z]+;
NUMBER      : [0-9]+;
Skip        : [ \t\n\r]+ -> skip;

Result is

result

Sign up to request clarification or add additional context in comments.

Comments

0

Java regular expressions do not support recursion, which is required for arbitrary nested parenthesis matching in a regex. Even if it did, the best solution would be to actually parse the expression.

If you don't want to use a proper parser, then you should tokenize it. Split it into a stream of function starts, parameters, and function ends, and then add logic to process that stream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.