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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:19:42+00:00 2026-06-12T21:19:42+00:00

If I create a RegEx from an empty string, how do I make it

  • 0

If I create a RegEx from an empty string, how do I make it match nothing?

I was looking for an option like RegexOptions.DoNotMatchWithEmpty or something like that.

To clarify:

Let’s assume I have no control over the string that is used to create the RegEx. The string can be empty and I have to create the RegEx object. I probably could set the RegEx to null and add a lot of null checking upstream but I’d rather not do that. Best solution would have been a RegexOptions flag that tells to match nothing when RegEx is constructed with an empty string. But that isn’t available.

  • 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-12T21:19:44+00:00Added an answer on June 12, 2026 at 9:19 pm

    How about just doing this

    if (string.IsNullOrEmpty(regex))
    {
        return false;
    }
    // standard regex lookup goes here
    

    Update: Alternatively just return a regular expression that matches nothing

    if (string.IsNullOrEmpty(regex))
    {
        return new Regex("a^");
    }
    return new Regex(regex);
    

    It works because the ^ character matches the start of the string being tested (unless the Multiline regex option is supplied, which it isn’t), however this clearly can’t match as we have already matched another character, a.

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

Sidebar

Related Questions

I'd like to create a regex that will match an opening <a> tag containing
I want to create array of regex match string from the text bellow. .title1
How can I create an XPath expression that works like a regex would, so
Is there any way to create the regex /func:\[sync\] displayPTS/ from string func:[sync] displayPTS
I would like to remove excessive empty lines from a string, but allow one
I'm trying to create a regex that takes all the content from <div class=entrytext>
I am trying to create a PHP REGEX that will match if two words
I'm trying to create a string from a regular expression. I noticed that in
I would like to create a regex to validate customer names. This would be
I'm trying to create a regex to tokenize a string. An example of the

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.