java - Spliting by regex or ebnf -
i've got string like:
create person +fname : string, +lname: string, -age:int;
is there possibility split regex or ebnf? mean things [a-za-z0-9]
(things don't know) stored in array?
in other words, using regexp:
^create [a-za-z][a-za-z0-9]* [s|b]?[+|[-]|=][a-za-z][a-za-z0-9]*[ ]?:[ ]?[a-za-z][a-za-z0-9]*(, [s|b]?[+|[-]|=][a-za-z][a-za-z0-9]*[ ]?:[ ]?[a-za-z][a-za-z0-9]*)*;
i want obtain array:
- person
- +
- fname
- string
- +
- lname
- string
- -
- age
- int
regards
you can try split way
string[] tokens = "create person +fname : string, +lname: string, -age:int;" .split("[\\s:;,]+|(?<=[+\\-])"); //split on set of characters containing spaces:;, or after + or -. (string s : tokens) system.out.println("=> " + s);
output:
=> create => person => + => fname => string => + => lname => string => - => age => int
as can see put create
@ start of array start iterating tokens[1]
.
you try add ^create\\s
part of splitting rule, produce empty string @ start of tokens array, won't solve anything.
Comments
Post a Comment