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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:05:40+00:00 2026-05-17T22:05:40+00:00

I am trying to apply negation on regular expression in .Net. It does not

  • 0

I am trying to apply negation on regular expression in .Net. It does not work. When string has valid last name reg ex should not match. For invalid last name it should match. Valid name allows only charecters, spaces, single quote and length between 1-40. Somebody suggested to parse the XML, I don’t want to do that. I know there is another way of doing this by removing the negation in reg ex and invert the match condition in code. But I don’t want that too. I need pure reg ex solution for this.

Here is my code. That does match the valid last name. But I don’t want to match.

string toBevalidated = @"<FirstName>SomeName</FirstName><LastName>Some</LastName><Address1>Addre1</Address1>";
        var regex = new Regex(@"<LastName>([^a-zA-Z'\s])|(.{41,})</LastName>");
        var match = regex.Match(toBevalidated);

        // Check to see if a match was found
        if (match.Success)
        {
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine("Failed");
        }

EDIT:
There are confusion here let me give some example what I intended to to. when last name is valid reg ex should not match. For example below samples should not match the reg ex

case 1

<FirstName>SomeName</FirstName><LastName>Brian</LastName><Address1>Addre1</Address1>

Case 2

<FirstName>SomeName</FirstName><LastName>O'neil</LastName><Address1>Addre1</Address1>

case 3

<FirstName>SomeName</FirstName><LastName>Peter John</LastName><Address1>Addre1</Address1>

When last name is invalid, reg ex should match

case 4

<FirstName>SomeName</FirstName><LastName>Brian123</LastName><Address1>Addre1</Address1>

case 5

<FirstName>SomeName</FirstName><LastName>#Brian</LastName><Address1>Addre1</Address1>

case 6

<FirstName>SomeName</FirstName><LastName>BrianBrianBrianBrianBrianBrianBrianBrianBrianBrian</LastName><Address1>Addre1</Address1>

if you need more information please let me know

  • 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-17T22:05:41+00:00Added an answer on May 17, 2026 at 10:05 pm

    It would have been helpful if you’d given an example of this not behaving as you expected it to, but I suspect it’s because you’re only matching an invalid character if it’s a single invalid character, e.g.

    <LastName>5</LastName>
    

    That will match (I believe; I haven’t checked) but this won’t:

    <LastName>55</LastName>
    

    I think you could do something like:

    <LastName>(.*[^a-zA-Z'\s].*)|(.{41,})</LastName>
    

    to ensure that there’s at least one invalid character in there (or that there are 41 or more characters). But there may be corner cases here where that’s inappropriate.

    EDIT: Got it. The alternation operator was taking everything before it as an option, instead of just the preceding group. The final regular expression is:

    <LastName>((.*[^a-zA-Z'\s].*)|(.{41,}))</LastName>
    

    And here’s some sample code:

    using System;
    using System.Text.RegularExpressions;
    
    class Test
    {
        static void Main()
        {
            string pattern = @"<LastName>((.*[^a-zA-Z'\s].*)|(.{41,}))</LastName>";
            Regex regex = new Regex(pattern);
    
            string[] samples = {
                "<FirstName>SomeName</FirstName><LastName>Brian</LastName><Address1>Addre1</Address1>",
                "<FirstName>SomeName</FirstName><LastName>O'neil</LastName><Address1>Addre1</Address1>",
                "<FirstName>SomeName</FirstName><LastName>Peter John</LastName><Address1>Addre1</Address1>",
                "<FirstName>SomeName</FirstName><LastName>Brian123</LastName><Address1>Addre1</Address1>",                
                "<FirstName>SomeName</FirstName><LastName>#Brian</LastName><Address1>Addre1</Address1>",
                "<FirstName>SomeName</FirstName><LastName>BrianBrianBrianBrianBrianBrianBrianBrianBrianBrian</LastName><Address1>Addre1</Address1>",
            };
    
            foreach (var sample in samples)
            {
                bool valid = !regex.IsMatch(sample);
                Console.WriteLine("Valid: {0} Text: {1}", valid, sample);
            }
        }
    }
    
    • 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.