regular expression match empty string

Posted on 2011-05-02
Last Modified: 2012-05-11
Hello -

I need a regular expression that will tell me if the string I am looking at is empty.

Question by:DTwined
    LVL 11

    Accepted Solution

    The following expression expects nothing to be matched (an empty string).

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    __NO POINTS__

    ... provided you don't turn on the multi-line modifier.

    Author Comment

    The string is one line.
    LVL 11

    Expert Comment

    @DTwined: Because you are using a single line string the above code will work. Depending on your programming language though, there are probably better ways to do this with trimming and checking for zero length. Some languages provide helper methods like string.IsNullOrEmpty and string.IsNullOrWhiteSpace in .NET.

    @kaufmed: If the string was multi-line then it actually contains characters (carriage returns and/or line feeds). The above regular expression means it genuinely empty.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    If the string was multi-line then it actually contains characters
    I didn't say the string would be multi-line; I said the multi-line modifier is turned on, which changes the meaning of ^ and $. However, when I tested it didn't seem to make a difference, so my comment above may be moot anyway  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now