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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:14:30+00:00 2026-06-15T00:14:30+00:00

I have been trying to write a code that will check if the given

  • 0

I have been trying to write a code that will check if the given string contains certain strings with certain pattern.
To be precise, for example:

string mainString = @"~(Homo Sapiens means (human being)) or man or ~woman"
List<string> checkList = new List<string>{"homo sapiens","human","man","woman"};

Now, I want to extract

"homo sapiens", "human" and "woman" but NOT "man"

from the above list as they follow the pattern, i.e string followed by~ or one of the strings inside parenthesis that starts with ~.
So far I have come up with:

string mainString = @"~(Homo Sapiens means (human being)) or man or ~woman"
List<string> checkList = new List<string>{"homo sapiens","human","man","woman"};
var prunedList = new List<string>();
foreach(var term in checkList)
{
   var pattern = @"~(\s)*(\(\s*)?(\(?\w\s*\)?)*" + term + @"(\s*\))?";
   Match m = Regex.Match(mainString, pattern);
   if(m.success)
   {
      prunedList.Add(term);
   }
 }

But this pattern is not working for all cases…
Can any one suggest me how this can be done?

  • 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-15T00:14:32+00:00Added an answer on June 15, 2026 at 12:14 am

    Paranthesis checking is a context-free language or grammar which requires a stack for checking. Regular expressions are suitable for regular languages. They do not have memory, therefore they cannot be used for such purposes.

    To check this you need to scan the string and count the parentheses:

    • initialize count to 0
    • scan the string
      • if current character is ( then increment count
      • if current character is ) then decrement count
      • if count is negative, raise an error that parentheses are inconsistent; e.g., )(
    • In the end, if count is positive, then there are some unclosed parenthesis
    • If count is zero, then the test is passed

    Or in C#:

    public static bool CheckParentheses(string input)
    {
        int count = 0;
        foreach (var ch in input)
        {
            if (ch == '(') count++;
            if (ch == ')') count--;
    
            // if a parenthesis is closed without being opened return false
            if(count < 0)
                return false;
        }
    
        // in the end the test is passed only if count is zero
        return count == 0;
    }
    

    You see, since regular expressions are not capable of counting, then they cannot check such patterns.

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

Sidebar

Related Questions

I have been trying to write a regex that will remove whitespace following a
I have been trying to write some code in Scala to read a file
I'm trying to write a piece of code that will do the following: Take
I am trying to write code that will fetch the files in a directory
I'm trying to write some code that will render a texture onto a simple
I've been working on trying to write a function that will grab the POST
i have been trying to write a program in python to read two text
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but
I have been trying to write my own diff3 wrap script for SVN 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.