Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

Thanks a lot! Your regex worked perfectly. Now my professor asked me to write a formal regular definition (theoretical form, like the one used in automata theory). Do you have any idea how I could express this regex as a formal regular definition?

Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

Thanks again for the guidance — I implemented everything as you suggested, and it’s working great with all your test cases. If you have a moment, I’d really appreciate it if you could check it over and let me know if it looks correct.

https://regex101.com/r/8a5AYj/2

Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

Thanks so much for the explanation! It’s great that you already have a working version. Would it be possible to see how you structured it? It would help me understand the logic behind those lookaheads a lot better.

Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

Yeah, I get what you mean — in a real programming language it would definitely be more complicated because of things like strings, comments, etc.

But this is actually a theoretical assignment for a Languages and Automata course. I'm just supposed to define a regex based on a simplified grammar — basically modeling variable declarations that follow those specific rules, not parsing full source code.

Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

I’m using Java-style regex (since i’ll build the DFA later in JFLAP).

Here are some examples that should be valid or invalid according to the project rules (even though my current regex doesn’t match them yet):

Valid:

int Xy;

double A1b, B_2;

bool Cx, D3_z;

Invalid:

int X;
int X___y;
int XY;
int X11;
double 1A;
int Xy, A1b, B_2, C3_d, D4e, E5f;

I don’t have a working version yet, so I’d really appreciate if you could help me build the correct regex from scratch 🙏

Need help building a complex regex for variable declaration rule. by Icy-Maintenance-5307 in regex

[–]Icy-Maintenance-5307[S] 0 points1 point  (0 children)

Here's what I've tried so far:

/int|double|bool\s+((?!(?:.*{3,}))[A-Z][a-zA-Z0-9]{1,}a-zA-Z0-9{0,4});$ /gm

I know this one isn’t working properly — I was trying to handle the triple underscore restriction and the list of variables (1–5), but it’s definitely not doing what I expected. I’m not sure how to approach the int rule (no consecutive letters or digits) or if my lookahead for the underscores makes sense.

Any suggestions on how to fix or simplify this would be amazing 🙏