0

/[a-z\s]+[A-Za-z][A-Za-z0-9]*\,?[\sA-Za-z,]+;/

int A;

(int space A) is passing in the online regex101.com but i am not getting how it get passed as [a-z\s]+ this will process upto "int " and will move to next on A now A; is left A will pass the [A-Za-z] now ; left but [\sA-Za-z,]+ is still there and + means 1 or more so it should not pass.

Can someone explain where i am going wrong?

  • 2
    If you ever come across these longer regexps, just capture all their parts, `([a-z\s]+)([A-Za-z])([A-Za-z0-9]*)(,?)([\sA-Za-z,]+);`, and see [what each group matches](https://regex101.com/r/dAnPz3/1). – Wiktor Stribiżew Oct 15 '20 at 07:31
  • okay understood the first part stops at in but why it stops at in as it can go upto "int " as all the "int "satisfy the first part and when A come then only it does not satisfy so it will move to second part then. – Priyanshu Dangi Oct 15 '20 at 07:52
  • You need to read more on [backtracking in regex](https://docs.microsoft.com/en-us/dotnet/standard/base-types/backtracking-in-regular-expressions). You only use greedy quantifiers here, not possessive ones that cancel backtracking. – Wiktor Stribiżew Oct 15 '20 at 07:54

0 Answers0