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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:33:32+00:00 2026-05-13T01:33:32+00:00

I’m trying to filter thousands of files, looking for those which contain string constants

  • 0

I’m trying to filter thousands of files, looking for those which contain string constants with mixed case. Such strings can be embedded in whitespace, but may not contain whitespace themselves. So the following (containing UC chars) are matches:

"  AString "   // leading and trailing spaces together allowed
"AString "     // trailing spaces allowed
"  AString"    // leading spaces allowed
"newString03"  // numeric chars allowed
"!stringBIG?"  // non-alphanumeric chars allowed
"R"            // Single UC is a match

but these are not:

"A String" // not a match because it contains an embedded space
"Foo bar baz" // does not match due to multiple whitespace interruptions
"a_string" // not a match because there are no UC chars

I still want to match on lines which contain both patterns:

"ABigString", "a sentence fragment" // need to catch so I find the first case...

I want to use Perl regexps, preferably driven by the ack command-line tool. Obviously, \w and \W are not going to work. It seems that \S should match the non-space chars. I can’t seem to figure out how to embed the requirement of “at least one upper-case character per string”…

ack --match '\"\s*\S+\s*\"'

is the closest I’ve gotten. I need to replace the \S+ with something that captures the “at least one upper-case (ascii) character (in any position of the non-whitespace string)” requirement.

This is straightforward to program in C/C++ (and yes, Perl, procedurally, without resorting to regexps), I’m just trying to figure out if there is a regular expression which can do the same job.

  • 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-13T01:33:33+00:00Added an answer on May 13, 2026 at 1:33 am

    The following pattern passes all your tests:

    qr/
      "      # leading single quote
    
      (?!    # filter out strings with internal spaces
         [^"]*   # zero or more non-quotes
         [^"\s]  # neither a quote nor whitespace
         \s+     # internal whitespace
         [^"\s]  # another non-quote, non-whitespace character
      )
    
      [^"]*  # zero or more non-quote characters
      [A-Z]  # at least one uppercase letter
      [^"]*  # followed by zero or more non-quotes
      "      # and finally the trailing quote
    /x
    

    Using this test program—that uses the above pattern without /x and therefore without whitespace and comments—as input to ack-grep (as ack is called on Ubuntu)

    #! /usr/bin/perl
    
    my @tests = (
      [ q<"  AString ">   => 1 ],
      [ q<"AString ">     => 1 ],
      [ q<"  AString">    => 1 ],
      [ q<"newString03">  => 1 ],
      [ q<"!stringBIG?">  => 1 ],
      [ q<"R">            => 1 ],
      [ q<"A String">     => 0 ],
      [ q<"a_string">     => 0 ],
      [ q<"ABigString", "a sentence fragment"> => 1 ],
      [ q<"  a String  "> => 0 ],
      [ q<"Foo bar baz">  => 0 ],
    );
    
    my $pattern = qr/"(?![^"]*[^"\s]\s+[^"\s])[^"]*[A-Z][^"]*"/;
    for (@tests) {
      my($str,$expectMatch) = @$_;
      my $matched = $str =~ /$pattern/;
      print +($matched xor $expectMatch) ? "FAIL" : "PASS",
            ": $str\n";
    }
    

    produces the following output:

    $ ack-grep '"(?![^"]*[^"\s]\s+[^"\s])[^"]*[A-Z][^"]*"' try
      [ q<"  AString ">   => 1 ],
      [ q<"AString ">     => 1 ],
      [ q<"  AString">    => 1 ],
      [ q<"newString03">  => 1 ],
      [ q<"!stringBIG?">  => 1 ],
      [ q<"R">            => 1 ],
      [ q<"ABigString", "a sentence fragment"> => 1 ],
    my $pattern = qr/"(?![^"]*[^"\s]\s+[^"\s])[^"]*[A-Z][^"]*"/;
      print +($matched xor $expectMatch) ? "FAIL" : "PASS",
    

    With the C shell and derivatives, you have to escape the bang:

    % ack-grep '"(?\![^"]*[^"\s]\s+[^"\s])[^"]*[A-Z][^"]*"' ...
    

    I wish I could preserve the highlighted matches, but that doesn’t seem to be allowed.

    Note that escaped double-quotes (\") will severely confuse this pattern.

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

Sidebar

Related Questions

No related questions found

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.