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 875259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:14:20+00:00 2026-05-15T11:14:20+00:00

I want to match following pattern: key=value key=value key=value key=value … where key and

  • 0

I want to match following pattern:

key="value" key="value" key="value" key="value" ...

where key and value are [a-z0-9]+, both should be grouped (2 groups, the ” – chars can be matched or skipped)

input that should not be matched:
key=”value”key=”value” (no space between pairs)

For now I got this(not .NET syntax):

([a-z0-9]+)=(\"[a-z0-9]+\")(?=\s|$)

the problem with that, that it matches key4=”value4″ in input:

 key3="value3"key4="value4"
  • 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-15T11:14:21+00:00Added an answer on May 15, 2026 at 11:14 am

    The spec isn’t very clear, but you can try:

    (?<!\S)([a-z0-9]+)=("[a-z0-9]+")(?!\S)
    

    Or, as a C# string literal:

    "(?<!\\S)([a-z0-9]+)=(\"[a-z0-9]+\")(?!\\S)"
    

    This uses a negative lookarounds to ensure that the the key-value pair is neither preceded nor followed by non-whitespace characters.

    Here’s an example snippet (as seen on ideone.com):

       var input = "key1=\"value1\" key2=\"value2\"key3=\"value3\" key4=\"value4\"";
       Console.WriteLine(input);
       // key1="value1" key2="value2"key3="value3" key4="value4"
    
       Regex r = new Regex("(?<!\\S)([a-z0-9]+)=(\"[a-z0-9]+\")(?!\\S)");
       foreach (Match m in r.Matches(input)) {
         Console.WriteLine(m);
       }
       // key1="value1"
       // key4="value4"
    

    Related questions

    • How does the regular expression (?<=#)[^#]+(?=#) work?

    On validating the entire input

    You can use Regex.IsMatch to see if the input string matches against what should be the correct input pattern. You can also use the same pattern to extract the keys/values, thanks to the fact that .NET regex lets you access individual captures.

       string[] inputs = {
          "k1=\"v1\" k2=\"v2\" k3=\"v3\" k4=\"v4\"",
          "k1=\"v1\" k2=\"v2\"k3=\"v3\" k4=\"v4\"",
          "    k1=\"v1\"      k2=\"v2\"     k3=\"v3\"     k4=\"v4\"     ",
          "     ",
          " what is this? "
       };
    
       Regex r = new Regex("^\\s*(?:([a-z0-9]+)=\"([a-z0-9]+)\"(?:\\s+|$))+$");
       foreach (string input in inputs) {
         Console.Write(input);
         if (r.IsMatch(input)) {
            Console.WriteLine(": MATCH!");
            Match m = r.Match(input);
            CaptureCollection keys   = m.Groups[1].Captures;
            CaptureCollection values = m.Groups[2].Captures;
            int N = keys.Count;
            for (int i = 0; i < N; i++) {
               Console.WriteLine(i + "[" + keys[i] + "]=>[" + values[i] + "]");
            }
         } else {
            Console.WriteLine(": NO MATCH!");
         }
       }
    

    The above prints (as seen on ideone.com):

    k1="v1" k2="v2" k3="v3" k4="v4": MATCH!
    0[k1]=>[v1]
    1[k2]=>[v2]
    2[k3]=>[v3]
    3[k4]=>[v4]
    k1="v1" k2="v2"k3="v3" k4="v4": NO MATCH!
        k1="v1"      k2="v2"     k3="v3"     k4="v4"     : MATCH!
    0[k1]=>[v1]
    1[k2]=>[v2]
    2[k3]=>[v3]
    3[k4]=>[v4]
         : NO MATCH!
     what is this? : NO MATCH!
    

    References

    • Is there a regex flavor that allows me to count the number of repetitions matched by the * and + operators?

    Explanation of the pattern

    The pattern to validate the entire input is essentially:

    maybe leading
    spaces       ___ end of string anchor
      |         /
    ^\s*(entry)+$
    |          \
    beginning   \__ one or more entry
    of string
    anchor
    

    Where each entry is:

    key=value(\s+|$)
    

    That is, a key/value pair followed by either spaces or the end of the string.

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

Sidebar

Related Questions

If I want to match() a string with the following pattern: 12345678_12345678 What would
Heres my regex pattern: [A-Z][a-z]?-?([A-Z][a-z]?)? I want this regex to match the following: A
In my tokenizer (.lex) file I want to match the following pattern : AaBC12/awD41/dfs21
I have the following regex pattern to match: NSString *pattern=[NSString stringWithFormat:@%@(.*)%@, key, key2]; So
I want to match dates that have the following format: 2010-08-27, 2010/08/27 Right now
How to match the following i want all the names with in the single
I want to match and modify part of a string if following conditions are
Example: Suppose in the following example I want to match strings that do not
I have a question about the following regular expression: I want to match the
In the following text, I want to match only the first [ID], but not

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.