djfoxmccloud
djfoxmccloud

Reputation: 599

flex regex not matching properly

In my tokenizer (.lex) file I want to match the following pattern :

AaBC12/awD41/dfs21 etc...

I've written this rule

[A-Za-z]+[A-Za-z0-9]*[[/]+[A-Za-z][A-Za-z0-9]*]* 
{lline = cpflineno;cpflval.str = strdup(cpftext);return K_IDENTIFIER;}

This rule seems correct to me but if i have an input like this :

TOP/MD1 
TOP/MD2
TOP/MD2/D/E

My output is

TOP/MD1
TOP/MD2
TOP/MD2
/D/E  

instead of

TOP/MD1
TOP/MD2
TOP/MD2/D/E

Could you tell me where my rule fails ?

Upvotes: 0

Views: 371

Answers (1)

Qtax
Qtax

Reputation: 33928

What about this:

[A-Za-z]+[A-Za-z0-9]*([/]+[A-Za-z][A-Za-z0-9]*)* 

Replaced [] with () where you mean a group.

Note that it will match foo////bar, if you don't want that remove the second + (and the first + for that matter too, it's useless in this case).

Upvotes: 1

Related Questions