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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:39+00:00 2026-06-10T19:00:39+00:00

I’m working with an array of strings, and would like to do the following:

  • 0

I’m working with an array of strings, and would like to do the following:

//Regex regex; List<string> strList; List<string> strList2; 
foreach (string str in strList){
    if (regex.IsMatch(str)) {      //only need in new array if matches...
        strList2.Add(regex.Replace(str, myMatchEvaluator)) 
                                   //but still have to apply transformation
    }
}

Now, I know that works, but that effectively means running the same regex twice on each string in the array. Is there a way to collapse both of these steps – the filtering and the transformation – into one regex-parsing call?

(One that would work most of the time is

string str2 = regex.Replace(str, myMatchEvaluator);
if (str2 == str)
    strList2.Add(str2);

But that would often throw out some valid matches that still didn’t need replacement.)

EDIT: A regex example, roughly similar to mine, to illustrate why this is tricky:
Imagine looking for words at the beginning of lines in a log file, and wanting to capitalize them.

The regex would be new Regex("^[a-z]+", RegexOptions.IgnorePatternWhiteSpace), and the replacement function would be match => match.ToUpper().

Now some first words are already capitalized, and I don’t want to throw them away. On the other hand, I don’t want to upper-case all instances of the word on the line, just the first one.

  • 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-10T19:00:41+00:00Added an answer on June 10, 2026 at 7:00 pm

    Building off of all of the answers I’ve received, the following works:

    void AddToIfMatch(List<string> list, string str; Regex regex; 
                                            MatchEvaluator evaluator)
    {
        bool hasBeenEvaluated = false;
        string str2 = regex.Replace(
            str, 
            m => {HasBeenEvaluated = true; return evaluator(m);}
        );
        if( hasBeenEvaluated ) {list.Add(str2);}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.