Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression for string in c

Tags:

string

regex

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

like image 943
name_masked Avatar asked Feb 15 '26 07:02

name_masked


1 Answers

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

  • regular-expressions.info/Character class, alternation, grouping
like image 128
polygenelubricants Avatar answered Feb 17 '26 20:02

polygenelubricants



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!