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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:40:52+00:00 2026-05-13T18:40:52+00:00

If I have input string in C#, how can I do series of Regex

  • 0

If I have input string in C#, how can I do series of Regex / linq operations on it to do Regex match on one piece of string and then another Regex on all pieces of string which are not matched by 1st Regex.

In other words, for input string:

<!-- Lorem ipsum dolor sit amet, consectetur adipiscing elit -->  
<!-- The quick brown fox jumps over the lazy dog -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit 
The quick brown fox jumps over the lazy  dog
<!-- Lorem ipsum dolor sit amet, consectetur adipiscing elit -->  
<!-- The quick brown fox jumps over the lazy dog -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit
The quick brown fox jumps over the lazy dog

I want to use Regex1 to match lines with <!-- --> and do certain operation on them without parsing them further. And to have Regex2 to match things in pieces of string not matched with Regex1, for example to find all words “fox” and “dog” in those lines and do certain operations on those words.

What is the best way to combine Regex/linq operations in situation like this?

  • 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-13T18:40:52+00:00Added an answer on May 13, 2026 at 6:40 pm

    You’re in luck since .NET supports variable-length lookbehind.

    Therefore, you can use two regexes in sequence.

    First, use

    ^<!--(.*)-->\s*$
    

    to find all comment lines. Backreference $1 will contain whatever is between the delimiters. For example:

    Regex paragraphs = new Regex(@"^<!--(.*)-->\s*$", RegexOptions.Multiline);
    Match matchResults = paragraphs.Match(subjectString);
    while (matchResults.Success) {
        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    

    Second, to find and manipulate “dog” and “fox” in the other lines, you can use

    (?<!^<!--.*)(dog|fox)
    

    What this regex means is “Match dog or fox unless the line starts with <!--“. So if you want to replace them, say, by “cat”, use

    resultString = Regex.Replace(subjectString, "(?<!^<!--.*)(dog|fox)", "cat", RegexOptions.Multiline);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't understand how to solve the following problem: I have input string aaaabaa
I have an input string and a pattern. I need to replace each match
I have an input string: access_token=f34b46f0f109d423sd4236af12d1bce7f10df108ec2183046b8f94641ebe&amp;expires_in=0&amp;user_id=37917395 Source code: Regex regex = new Regex(access_token=([^&]+)); MatchCollection
I have an input string which is a directory address: Example: ProgramFiles/Micro/Telephone And I
I have an input string such as x=y|a=b|c=10 etc which gets converted to dynamic
I have a string input = "maybe (this is | that was) some ((nice
I have a string input that i do not know whether or not is
How to rewrite Webconfig connection string at runtime.I have input textbox for Server,UserName and
I have a string as input and have to break the string in two
I have an input field whose name is an MD5 string e.g.: <input type=hidden

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.