0

I'm doing a simple language sql with regex, and i'm parsing the sentence CREATE TABLE, but does not work. I use this:

reti = regcomp(&regex,  "CREATE TABLE [a-zA-Z]\\((.*)\\)", REG_EXTENDED);

It's something simple, i just do it to learn... What is wrong with the regex?

1
  • "Does not work" is not a useful problem description. Please provide sample input, and expected and actual output. Commented Dec 17, 2014 at 20:50

1 Answer 1

1

Apart from only working on one letter SQL tables, you are either omitting whitespaces before the open parentheses or, depending on the SQL and regex engine/string syntax, over-escaping your expression parentheses.

Check:

[A-Za-z]+\\s*

if it doesn't work and the plus sign is not recognized,

[A-Za-z][A-Za-z]*\\s*

and whether it's \\\\( or just \\( (it should be the latter. But better be sure).

This supports names such as Antinoo and cUsToMeRs, but not Invoices_New or Suppliers2014. You might want to add numbers and underscores to your regex. Since table names probably won't start with numbers, you'll want

[A-Z_a-z][A-Z_a-z0-9]*\\s*\\(([^;]*)\\)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for commeting. When i use "(" or "\s", the compiler gives me an error: warning: unknown escape sequence: '\s' [enabled by default]. therefore i use double "\\"
My bad. You did say you were using C. So \\s is correct, and \\( should be too (I seem to remember some scenario where I needed \\\\( but I can no longer recall why and where. Just in case, checking is easy).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.