Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3943058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:44:13+00:00 2026-05-20T00:44:13+00:00

I am trying to write a regular expression that will match a string that

  • 0

I am trying to write a regular expression that will match a string that contains name-value pairs of the form:

<name> = <value>, <name> = <value>, ...

Where <value> is a C# string literal. I already know the s that I need to find via this regular expression. So far I have the following:

regex = new Regex(fieldName + @"\s*=\s*""(.*?)""");

This works well, but it of course fails to match in the case where the string I am trying to match contans a <value> with an escaped quote. I am struggling to work out how to solve this, I think I need a lookahead, but need a few pointers. As an example, I would like to be able to match the value of the ‘difficult’ named value below:

difficult = "\\\a\b\'\"\0\f \t\v", easy = "one"

I would appreciate a decent explanation with your answers, I want to learn, rather than copy 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:44:14+00:00Added an answer on May 20, 2026 at 12:44 am

    Try this to capture the key and value:

    (\w+)\s*=\s*(@"(?:[^"]|"")*"|"(?:\\.|[^\\"])*")
    

    As a bonus, it also works on verbatim strings.

    C# Examples:https://dotnetfiddle.net/vQP4rn

    Here’s an annotated version:

    string pattern = @"
    (\w+)\s*=\s*    # key =
    (               # Capturing group for the string
        @""               # verbatim string - match literal at-sign and a quote
        (?:
            [^""]|""""    # match a non-quote character, or two quotes
        )*                # zero times or more
        ""                #literal quote
    |               #OR - regular string
        ""              # string literal - opening quote
        (?:
            \\.         # match an escaped character,
            |[^\\""]    # or a character that isn't a quote or a backslash
        )*              # a few times
        ""              # string literal - closing quote
    )";
    MatchCollection matches = Regex.Matches(s, pattern, 
                                            RegexOptions.IgnorePatternWhitespace);
    

    Note that the regular string allows all characters to be escaped, unlike in C#, and allows newlines. It should be easy to correct if you need validation, but it should be file for parsing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regular expression to match a string that may or
I am trying to write a regular expression to match ruby's list and hash
I'm trying to write a regular expression that replaces line feeds between certain areas
I am trying to write a regular expression in C# to remove all script
I'm trying to write a bash function which will take all the arguments sent
I'm trying to write a class that calls back certain functions for different browsers.
I'm trying to add tags to some given query strings, and the tags should
I have a directory which contains subdirectories which contain files. All the file names
You're probably thinking this has been asked a million times before but I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.