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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:33:55+00:00 2026-06-13T13:33:55+00:00

I am trying to use a regex to parse out the key-value pairs of

  • 0

I am trying to use a regex to parse out the key-value pairs of command-line switches. Here’s what I’ve got so far:

(?<=(^-{1,2}| -{1,2}|^/| /))(?<name>[\w]+)[ :"]*(?<value>[\w.?=&+ :/|\\]*)(?=[ "]|$)

It seems to parse everything properly… almost. If there are hyphens in the value, it craps out on the match. How do I tweak this to work on all the test examples below?

test examples (all valid):

-s  -i:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
-o:"C:\temp\db\" -s -r -host:localhost --d theDB
-s  -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB -Scripts\" -h:localhost -d:theDB
-s  -d http://www.theproject.com -h:localhost -d:theDB
-i:"C:\Users\Fozzie\Workspace\TheProject\TheProject_Stack_1_5\db\DB Scripts\" --h:localhost -d:theDB
-h:localhost -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack_1_5\db\DB Scripts\"  -d:theDB
--d theDB   -o:"C:\temp\db\" -host=local-host     -r

The regex fails when the value part is something like

"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\"

or "local-host" due to the hyphens therein. It is seen as the start of a new switch.

PS: I don’t want to use a canned options library like getops. I’m interested in getting the regex right.

Thanks.

UPDATE: Sorry for the missing detail: this is a .NET regex.

  • 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-06-13T13:33:56+00:00Added an answer on June 13, 2026 at 1:33 pm

    .NET solution — speculative

    This suggested .NET solution is just ‘suggested’; I don’t do .NET and have no way of testing it on any of my machines (is there a regex-test web site for .NET?). I’ve taken the working Perl solution, removed the <mark> and <pad> parts that you’re not worried about, and the comments, and flattened it all onto one line on the assumption that .NET doesn’t have an option for legibility analogous to Perl’s x option. You can still find 5 sets of parentheses corresponding to the 5 parts of the Perl regex. I’m assuming that (?:...) is a non-capturing group.

    (?:-{1,2}|/)(?<name>\w+)(?:[=:]?|\s+)(?<value>[^-\s"][^"]*?|"[^"]*")?(?=\s+[-/]|$)
    

    I also assume that .NET provides some mechanism analogous to Perl’s g modifier that allows you to scan the string on a second (or subsequent) pass where it left off on the previous pass. Or that you can somehow determine where the end of the match was and resume the scan from there.

    Perl solution — validated

    This is as good as I’ve managed to come up with using Perl regexes (tested with Perl 5.16.0 on Mac OS X 10.7.5).

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    # Original regex split into 5 sections:
    # C1          (?<=(^-{1,2}|\ -{1,2}|^/|\ /))
    # C2          (?<name>[\w]+)
    # C3          [ :"]*
    # C4          (?<value>[\w.?=&+ :/|\\]*)
    # C5          (?=[ "]|$)
    
    my $rx = qr%(?<mark>  -{1,2}|/ )                        (?# Was C1)
                (?<name>  \w+ )                             (?# Was C2)
                (?<pad>   (?: [=:]?|\s+ ))                  (?# Was C3)
                (?<value> (?: [^-\s"][^"]*? | "[^"]*" ))?   (?# Was C4)
                (?=\s+[-/]|$)                               (?# Was C5)
               %x;
    
    while (my $line = <DATA>)
    {
        chomp $line;
        print "\nLine: $line\n";
        while ($line =~ m/$rx/g)
        {
            my($mark, $name, $pad, $value) = ($1, $2, $3, $4 // "");
            print "Found: mark $mark name <<$name>> pad <<$pad>> value <<$value>>\n";
        }
    }
    
    __DATA__
    -s  -i:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    -o:"C:\temp\db\" -s -r -host:localhost --d theDB
    -s  -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB -Scripts\" -h:localhost -d:theDB
    -s  -d http://www.theproject.com -h:localhost -d:theDB
    -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject_Stack_1_5\db\DB Scripts\" --h:localhost -d:theDB
    -h:localhost -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack_1_5\db\DB Scripts\"  -d:theDB
    --d theDB   -o:"C:\temp\db\" -host=local-host     -r
    -s -i:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    /d theDB   /o:"C:\temp\db\" /host=local-host     /r
    /d theDB /o:"C:\temp\db\" /host=local-host /r /t
    -s:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    

    The bulk of the script is not very interesting. The outer while loop reads the data section of the file (which is the material after the marker __DATA__) one line at a time, prints it for validation, then repeatedly runs the regex on the line to find the components (the marker, the name, the padding, and the value), printing those out. The bulk of the data is what was provided in the question (thank you!). The last three lines of the data are extra compared to what was originally provided.

    All the excitement is in the regex. I’ve used Perl’s /x modifier to allow white space in the regex for readability. This means that white space is not significant unless preceded by a backslash or enclosed in square brackets (and there is no significant white space in this specimen). I’ve used the (?<name> ...) notation to identify the pieces as in the original, though the names could be omitted since they aren’t used. The (?# Was Cn) parts are pure comment.

    1. The mark is either one or two dashes or a slash; --? would be another, shorter way to write it.
    2. The name is a string of alphanumerics; this does not attempt to enforce ‘first character may not be a digit’.
    3. The pad separates the name from the value. It can be a single equals or colon, or a string of white space. The inner (?: ...) is a non-capturing grouping operator.
    4. The value is optional (the -s option in the first position of the first line of the sample data doesn’t have a value). It consists of: either a string starting with something other than a dash, double quote or white space, followed by a non-greedy string of non-quotes; or a double quote, a string of non-quotes, and another double quote.
    5. The trailing zero-width context (C5) is either one or more white space characters followed by a dash or slash, or EOS. Because the value pattern is non-greedy, the greedy trailing context gobbles the trailing white space after an option value.

    The output is:

    Line: -s  -i:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    Found: mark - name <<s>> pad << >> value <<>>
    Found: mark - name <<i>> pad <<:>> value <<C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\>>
    Found: mark - name <<h>> pad <<:>> value <<local:host>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: -o:"C:\temp\db\" -s -r -host:localhost --d theDB
    Found: mark - name <<o>> pad <<:>> value <<"C:\temp\db\">>
    Found: mark - name <<s>> pad <<>> value <<>>
    Found: mark - name <<r>> pad <<>> value <<>>
    Found: mark - name <<host>> pad <<:>> value <<localhost>>
    Found: mark -- name <<d>> pad << >> value <<theDB>>
    
    Line: -s  -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB -Scripts\" -h:localhost -d:theDB
    Found: mark - name <<s>> pad << >> value <<>>
    Found: mark - name <<i>> pad <<:>> value <<"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB -Scripts\">>
    Found: mark - name <<h>> pad <<:>> value <<localhost>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: -s  -d http://www.theproject.com -h:localhost -d:theDB
    Found: mark - name <<s>> pad << >> value <<>>
    Found: mark - name <<d>> pad << >> value <<http://www.theproject.com>>
    Found: mark - name <<h>> pad <<:>> value <<localhost>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject_Stack_1_5\db\DB Scripts\" --h:localhost -d:theDB
    Found: mark - name <<i>> pad <<:>> value <<"C:\Users\Fozzie\Workspace\TheProject\TheProject_Stack_1_5\db\DB Scripts\">>
    Found: mark -- name <<h>> pad <<:>> value <<localhost>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: -h:localhost -i:"C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack_1_5\db\DB Scripts\"  -d:theDB
    Found: mark - name <<h>> pad <<:>> value <<localhost>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: --d theDB   -o:"C:\temp\db\" -host=local-host     -r
    Found: mark -- name <<d>> pad << >> value <<theDB>>
    Found: mark - name <<o>> pad <<:>> value <<"C:\temp\db\">>
    Found: mark - name <<host>> pad <<=>> value <<local-host>>
    Found: mark - name <<r>> pad <<>> value <<>>
    
    Line: -s -i:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    Found: mark - name <<s>> pad <<>> value <<>>
    Found: mark - name <<i>> pad <<:>> value <<C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\>>
    Found: mark - name <<h>> pad <<:>> value <<local:host>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    Line: /d theDB   /o:"C:\temp\db\" /host=local-host     /r
    Found: mark / name <<d>> pad << >> value <<theDB  >>
    Found: mark / name <<o>> pad <<:>> value <<"C:\temp\db\">>
    Found: mark / name <<host>> pad <<=>> value <<local-host    >>
    Found: mark / name <<r>> pad <<>> value <<>>
    
    Line: /d theDB /o:"C:\temp\db\" /host=local-host /r /t
    Found: mark / name <<d>> pad << >> value <<theDB>>
    Found: mark / name <<o>> pad <<:>> value <<"C:\temp\db\">>
    Found: mark / name <<host>> pad <<=>> value <<local-host>>
    Found: mark / name <<r>> pad <<>> value <<>>
    Found: mark / name <<t>> pad <<>> value <<>>
    
    Line: -s:C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\ -h:local:host -d:theDB
    Found: mark - name <<s>> pad <<:>> value <<C:\Users\Fozzie\Workspace\TheProject\TheProject-Stack-1_5\db\DB Scripts\>>
    Found: mark - name <<h>> pad <<:>> value <<local:host>>
    Found: mark - name <<d>> pad <<:>> value <<theDB>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use regex to parse out tags. I've got the regex almost
I am trying to use regex to parse out some lines from text read
I'm trying to use regex to parse some plain text and add a definition
I'm trying to parse instructions passed in. I want to use regex to validate
I'm trying to write a regex that will parse out the directory and filename
trying to use regex to replace any white space with &nbsp;, inside of example
I am trying to use regex generators to create an expression, but I can't
I'm trying to use regex to check if a full address string contains a
I'm trying to use regex to match a string that starts with a <p>
I'm trying to use regex as the conditional in a Ruby (1.9.2) if statement

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.