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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:56:33+00:00 2026-05-27T20:56:33+00:00

I asked the original question here , and got a practical response with mixed

  • 0

I asked the original question here, and got a practical response with mixed Ruby and Regular Expressions. Now, the purist in me wants know: Can this be done in regular expressions? My gut says it can. There’s an ABNF floating around for bash 2.0, though it doesn’t include string escapes.

The Spec

Given an input line that is either (1) a variable (“key”) assignment from a bash-flavored script or (2) a key-value setting from a typical configuration file like postgresql.conf, this regex (or pair of regexen) should capture the key and value in such a way that I can use those captures to substitute a new value for that key.

You may use a different regular expression for shell-flavored and config-flavored lines; the caller will know which to use.

There will be a 50-point bounty here. I can’t add a bounty for two days, so I won’t accept an answer till then, but you can start answering immediately. You earn points for:

  • Readability (named capture groups, definitions via ?(DEFINE) or {0})
  • Using a single regex instead of two
  • Teaching me something about DFA
  • Regex performance, if relevant
  • Getting upvoted
  • First to use a technique

Example:

Given the input

export RAILS_ENV=production

I should be able to write in Ruby:

match = THE_REGEX.match("export RAILS_ENV=production")
newline = "export #{match[:key]}=#{match[:value]}"

Test cases: shell style

RAILS_ENV=development     # Don't forget to change this for TechCrunch
HOSTNAME=`cat /etc/hostname`
plist=`cat "/Applications/Sublim\`e Text 2.app/Content's/Info.plist"`

# Optional bonus input: "#" present in the string
FORMAT="  ##0.00 passe\`" #comment

Test cases: config style

listen_addresses = 127.0.0.1 #localhost only by default
# listen_addresses = 0.0.0.0 commented out, should not match

For the purpose of this challenge, “regular expression” and “regex” mean the same thing and both can refer to any common flavor you like, though I prefer Ruby 1.9-compatible.

  • 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-27T20:56:33+00:00Added an answer on May 27, 2026 at 8:56 pm

    I’m not sure about the full specs and what exactly you want in the value capturing group, but this should work for your test cases:

    /
    ^\s*+
    
    (?:export\s++)?
    (?<key>\w++)
    
    \s*+
    =
    \s*+
    
    (?<value>
      (?>  "(?:[^"\\]+|\\.)*+"
      |    '(?:[^'\\]+|\\.)*+'
      |    `(?:[^`\\]+|\\.)*+`
      |    [^#\n\r]++
      )
    )
    
    \s*+
    (?:#.*+)?
    $
    /mx;
    

    Handles comments and quotes with escapes.

    Perl/PCRE flavor and quoting.


    Example usage in Perl:

    my $re = qr/
        ^\s*+
    
        (?:export\s++)?
        (?<key>\w++)
    
        \s*+
        =
        \s*+
    
        (?<value>
          (?>  "(?:[^"\\]+|\\.)*+"
          |    '(?:[^'\\]+|\\.)*+'
          |    `(?:[^`\\]+|\\.)*+`
          |    [^#\n\r]++
          )
        )
    
        \s*+
        (?:\#.*+)?
        $
    /mx;
    
    my $str = <<'_TESTS_';
    RAILS_ENV=development     # Don't forget to change this for TechCrunch
    HOSTNAME=`cat /etc/hostname`
    plist=`cat "/Applications/Sublim\`e Text 2.app/Content's/Info.plist"`
    
    # Optional bonus input: "#" present in the string
    FORMAT="  ##0.00 passe\`" #comment
    
    listen_addresses = 127.0.0.1 #localhost only by default
    # listen_addresses = 0.0.0.0 commented out, should not match
    
    TEST="foo'bar\"baz#"
    TEST='foo\'bar"baz#\\'
    _TESTS_
    
    
    for(split /[\r\n]+/, $str){
        print "line: $_\n";
        print /$re/? "match: $1, $2\n": "no match\n";
        print "\n";
    }
    

    Output:

    line: RAILS_ENV=development     # Don't forget to change this for TechCrunch
    match: RAILS_ENV, development
    
    line: HOSTNAME=`cat /etc/hostname`
    match: HOSTNAME, `cat /etc/hostname`
    
    line: plist=`cat "/Applications/Sublim\`e Text 2.app/Content's/Info.plist"`
    match: plist, `cat "/Applications/Sublim\`e Text 2.app/Content's/Info.plist"`
    
    line: # Optional bonus input: "#" present in the string
    no match
    
    line: FORMAT="  ##0.00 passe\`" #comment
    match: FORMAT, "  ##0.00 passe\`"
    
    line: listen_addresses = 127.0.0.1 #localhost only by default
    match: listen_addresses, 127.0.0.1
    
    line: # listen_addresses = 0.0.0.0 commented out, should not match
    no match
    
    line: TEST="foo'bar\"baz#"
    match: TEST, "foo'bar\"baz#"
    
    line: TEST='foo\'bar"baz#\\'
    match: TEST, 'foo\'bar"baz#\\'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked many times, but here are my specific
Jeff Atwood asked the original question about parameterizing a SQL IN clause , but
(The original question was asked there : http://www.ogre3d.org/phpBB2/viewtopic.php?t=44832 ) Someone asked : While I
(I asked this question in another way , and got some interesting responses but
Ok, i kind of asked the wrong question so I've edited the original question.
I asked this question before but with less information than I have now. What
I've asked here a question about Task Killers and widgets stop working ( SO
I asked a question about interfaces previously and got some excellent responses. I'm really
I had asked the same question for iOS on iPad but now I am
Note: Full working example now below. Original question follows: I'm having problems using ld's

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.