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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:43:48+00:00 2026-05-12T21:43:48+00:00

I have a requirement. I have a text which can contain any characters. a)

  • 0

I have a requirement.

I have a text which can contain any characters.

a) I have to retain only Alphanumeric characters
b) If the word “The” is found with a space prefixed or suffixed with the word, that needs to be removed.

e.g.

CASE 1:

 Input:  The Company Pvt Ltd. 

 Output: Company Pvt Ltd

But 

     Input:  TheCompany Pvt Ltd. 

     Output: TheCompany Pvt Ltd

because there is no space between The & Company words.

CASE 2:

Similarly, Input:  Company Pvt Ltd.  The 

     Output: Company Pvt Ltd

But Input:  Company Pvt Ltd.The 

     Output: Company Pvt Ltd

Case 3:

Input: Company@234 Pvt; Ltd.

Output: Company234 Pvt Ltd

No , or . or any other special characters.

I am basically setting the data to some variable like

 _company.ShortName = _company.CompanyName.ToUpper();

So at the time of saving I cannot do anything. Only when I am getting the data from the database, then I need to apply this filter. The data is coming in _company.CompanyName

and I have to apply the filter on that.

So far I have done

public string ReplaceCharacters(string words)
{
    words = words.Replace(",", " ");
    words = words.Replace(";", " ");
    words = words.Replace(".", " ");
    words = words.Replace("THE ", " ");
    words = words.Replace(" THE", " ");
    return words;
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(ReplaceCharacters(textBox1.Text.ToUpper()));
}

Thanks in advance. I am using C#

  • 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-12T21:43:49+00:00Added an answer on May 12, 2026 at 9:43 pm

    Here is a basic regex that matches your supplied cases. With the caveat that as Kobi says, your supplied cases are inconsistent, so I’ve taken the periods out of the first four tests. If you need both, please add a comment.

    This handles all the cases you require, but the rapid proliferation of edge cases makes me think that maybe you should reconsider the initial problem?

        [TestMethod]
        public void RegexTest()
        {
            Assert.AreEqual("Company Pvt Ltd", RegexMethod("The Company Pvt Ltd"));
            Assert.AreEqual("TheCompany Pvt Ltd", RegexMethod("TheCompany Pvt Ltd"));
            Assert.AreEqual("Company Pvt Ltd", RegexMethod("Company Pvt Ltd. The"));
            Assert.AreEqual("Company Pvt LtdThe", RegexMethod("Company Pvt Ltd.The"));
            Assert.AreEqual("Company234 Pvt Ltd", RegexMethod("Company@234 Pvt; Ltd."));
            // Two new tests for new requirements
            Assert.AreEqual("CompanyThe Ltd", RegexMethod("CompanyThe Ltd."));
            Assert.AreEqual("theasdasdatheapple", RegexMethod("the theasdasdathe the the the ....apple,,,, the"));
            // And the case where you have THETHE at the start
            Assert.AreEqual("CCC", RegexMethod("THETHE CCC"));
        }
    
        public string RegexMethod(string input)
        {   
            // Old method before new requirement          
            //return Regex.Replace(input, @"The | The|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase);  
            // New method that anchors the first the          
            //return Regex.Replace(input, @"^The | The|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase);            
            // And a third method that does look behind and ahead for the last test
            return Regex.Replace(input, @"^(The)+\s|\s(?<![A-Z0-9])[\s]*The[\s]*(?![A-Z0-9])| The$|[^A-Z0-9\s]", string.Empty, RegexOptions.IgnoreCase);
        }
    

    I’ve also added a test method to my example that exercises the RegexMethod that contains the regular expression. To use this in your code you just need the second method.

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

Sidebar

Related Questions

Can i add two header text in Datagrid? My Requirement is to have two
I have a datatable being built in code which can dynamically have any given
I have a requirement to display a somewhat large amount of text, read only
I have a requirement in which a Text field has to be made editable
We have a requirement, in which we need to create dynamic word document and
I have a requirement to highlight a part of text based on certain criteria.
I have a requirement where i need to enter a text in the text
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I have requirement to get Facebook friends list with their images. How can I
I have a requirement to put together an application which will generate a web

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.