I'm working on a side project (trying to learn regex and get better at parsing in general) and trying to write a function that will validate if a string is valid under a particular grammar. The grammar is as follows:
statement -> delimeter token
delimeter -> / or -
token -> name ([check])* (delimeter token)?
check -> token
@id="..."
I've written out regular expressions for each of the above (except token), they are written below. However, when I tried to write out the token regex, I realized that it depends on itself (recursive). I'm not too sure how to write this regex or if this is even the correct way to go about it anymore, since check can go very deep potentially. Is there a better way to validate if the string can be represented by the grammar or no? If not, how do I do this with regex?
String delimeter = "/|-";
String name = "((?i)\\A[a-z][_a-z\\d\\-\\.]*){1}";
String checkToken = would just be equal to token;
String checkID = "(?i)\\A\\s*@id\\s*=\\s*\".*\"\\s*\\Z";
I'm using the String.matches call to see if a string matches the regex, right now just checking smaller things, like if a name is correct.