0

I'm looking to make a regular expression for some strings in C.

This is what i have so far:

Strings in C are delimited by double quotes (") so the regex has to be surrounded by \" \".

The string may not contain newline characters so I need to do [^\n] ( I think ).

The string may also contain double quotes or back slash characters if and only if they're escaped. Therefore [\\ \"] (again I think).

Other than that anything else goes.

Any help is much appreciated I'm kind of lost on how to start writing this regex.

8
  • Are you trying to match all strings without newline characters that may or may not have escaped back slashes and escaped double quotes? Commented Sep 19, 2017 at 1:58
  • Possible duplicate of Regular expression for a string literal in flex/lex Commented Sep 19, 2017 at 1:58
  • Yes that is correct @ N Brown Commented Sep 19, 2017 at 2:25
  • @Ken Y-N that posts asks a similar question but none of the answer complete the question as it still allows for a newline character. Commented Sep 19, 2017 at 2:28
  • This answer specifically says it is for single lines. Commented Sep 19, 2017 at 2:35

1 Answer 1

5

A simple flex pattern to recognize string literals (including literals with embedded line continuations):

["]([^"\\\n]|\\.|\\\n)*["]

That will allow

   "string with \
line continuation"

But not

"C doesn't support
 multiline strings"

If you don't want to deal with line continuations, remove the \\\n alternative. If you need trigraph support, it gets more irritating.

Although that recognizes strings, it doesn't attempt to make sense of them. Normally, a C lexer will want to process strings with backslash sequences, so that "\"\n" is converted to the two characters "NL (0x22 0x0A). You might, at some point, want to take a look at, for example, Optimizing flex string literal parsing (although that will need to be adapted if you are programming in C).

Flex patterns are documented in the flex manual. It might also be worthwhile reading a good reference on regular expressions, such as John Levine's excellent book on Flex and Bison.

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

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.