3

I am working writing a regular expression used to validate string in C. Here is to what I have gone so far

'^"[A-Za-z0-9]*[\t\n]*"$'

for rules - A string should begin with double quotes - May not contain a newline character

However, I am not able to capture the rule for allowing '\' or '"' in a string if preceded with '\'. Here is what I tried:

'^"[A-Za-z0-9]*[\t\n]*[\\\|\\"]?"$'

But this doesn't seem to work. What might be wrong with the regular expression here?

Regards, darkie15

1
  • What language are you doing the regex from? Commented May 30, 2010 at 5:01

2 Answers 2

2

You're misusing character classes and alternations in group; [\\\|\\"] isn't what you think it is.

Try something like this:

^"([A-Za-z0-9\t]|\\\\|\\")*"$

References

Sign up to request clarification or add additional context in comments.

Comments

0

If you want this regular expression:

^"[A-Za-z0-9]*[\t\n]*"$

To notate in C you must have double quotes around the string. Then you must escape the escapes and the double quotes inside the expression.

Here's what you might have:

"^\"[A-Za-z0-9]*[\\t\\n]*\"$"

Comments

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.