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

  • Home
  • SEARCH
  • 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 4024900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:46:45+00:00 2026-05-20T10:46:45+00:00

I have the following patterns: private static Regex rgxDefinitionDoMatch = new Regex(@d:(?<value>(?:(?!c:|d:|p:).)+), RegexOptions.Compiled); private

  • 0

I have the following patterns:

private static Regex rgxDefinitionDoMatch = new Regex(@"d:(?<value>(?:(?!c:|d:|p:).)+)", RegexOptions.Compiled);
private static Regex rgxDefinitionDontMatch = new Regex(@"\!d:(?<value>(?:(?!c:|d:|p:).)+)", RegexOptions.Compiled);
private static Regex rgxDefinitionExactDoMatch = new Regex(@"d:(?<value>\""(?:(?!c:|d:|p:).)+)\""", RegexOptions.Compiled);
private static Regex rgxDefinitionExactDontMatch = new Regex(@"\!d:(?<value>\""(?:(?!c:|d:|p:).)+)\""", RegexOptions.Compiled);

Here is an example string to match:

c:matchThis !c:dontMatchThis p:matchThis !p:dontMatchThis d:def !d:defDont d:"def" !d:"defDont"

Now here are some issues:

  1. When I use rgxDefinitionDontMatch, I get both !d:defDont and d:"defDont"
  2. When I use rgxDefinitionDoMatch it is even worse… I get !d:defDont, d:"defDont",
    !d:def and d:"def".

For number 2, I have tried different combinations to ignore the exclamation mark on the front of rgxDefinitionDoMatch ^(?!\!) for example, but it then just doesn’t match anything. I’m not sure what to do.

I will also need a way of ignoring quotes for both problems 1. and 2.

Can anyone help? I’ve been trying for some time now.

  • 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-20T10:46:46+00:00Added an answer on May 20, 2026 at 10:46 am

    Is this what you’re looking for?

    Regex[] rgxs = { 
      new Regex(@"(?<!\S)d:(?:""(?<value>[^""]+)""|(?<value>\S+))"),
      new Regex(@"(?<!\S)!d:(?:""(?<value>[^""]+)""|(?<value>\S+))")
    };
    
    string input = @"c:matchThis !c:dontMatchThis p:matchThis !p:dontMatchThis d:def !d:defDont d:""def"" !d:""defDont""";
    
    foreach (Regex r in rgxs)
    {
      Console.WriteLine(r.ToString());
      foreach (Match m in r.Matches(input))
      {
        foreach (String name in r.GetGroupNames())
        {
          Console.WriteLine("{0,-6} => {1}", name, m.Groups[name].Value);
        }
      }
      Console.WriteLine();
    }
    
    (?<!\S)d:(?:"(?<value>[^"]+)"|(?<value>\S+))
    0      => d:def
    value  => def
    0      => d:"def"
    value  => def
    
    (?<!\S)!d:(?:"(?<value>[^"]+)"|(?<value>\S+))
    0      => !d:defDont
    value  => defDont
    0      => !d:"defDont"
    value  => defDont

    As I was trying to figure out what you were asking, I finally decided the simplest course was to post my code and get your feedback. I’ll try to refine it as needed, and (of course) explain it. 😀


    EDIT: Here’s the separate regexes you asked for in the comments:

    Regex[] rgxs = { 
      new Regex(@"(?<!\S)d:(?<value>\S+)"),
      new Regex(@"(?<!\S)!d:(?<value>\S+)"),
      new Regex(@"(?<!\S)d:""(?<value>[^""]+)"""),
      new Regex(@"(?<!\S)!d:""(?<value>[^""]+)""")
    };
    

    Combining them the way I did, it doesn’t matter if the “value” part is quoted or not, it’s still captured–without the quotes, if they’re present. (I thought that’s what you meant by “ignoring quotes”.) What’s interesting about the combined form is how I used the same group name twice in the same regex– something few regex flavors support.

    (?<!\S), a negative lookbehind for a non-whitespace character, solves the question you posed in your comment: it insures that every match starts either at the beginning of the string or after a whitespace character. Similarly, the \S+ insures that the match continues ends at the end of the string or before the next whitespace character.

    "[^"]+", obviously, matches anything enclosed in quotes, except other quotes. It permits the value to contain whitespace, which I presumed was the reason for the separate regexes. But I mainly wanted to point out that you didn’t need to use backslashes to escape the quotes. In a C# verbatim string, it’s the extra quote that does the escaping: @"""[^""]+""".

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

Sidebar

Related Questions

I have the following regex in Java: private static final String ALPHA_REGEX = [^A-Za-z]+$;
I have the following pattern definition: private static final String D = (Mon|Tue|Wed|Thu|Fri|Sat|Sun); private
I have the following (Java) code: public class TestBlah { private static final String
I have the following servlet: public class MyServlet extends HttpServlet { private static final
I have a class with the following structure: class Something { private static $_instance
I have the following patterns and grouping application which I am refactoring in one.
I have the following method: protected BigDecimal stringToBigDecimal(String value) throws ParseException { if (Pattern.matches([\\d,.]+,
I have the following entry in a properties file: some.key = \n [1:Some value]
I'm using singleton design-pattern for my database connection. Here is my method. private static
first things first. I have the following classes: class Employee { private int employeeID;

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.