Regular Expression for Anything But an Empty String

regular expression for anything but an empty string


will match any string that contains at least one non-space character.


if (Regex.IsMatch(subjectString, @"^(?!\s*$).+")) {
// Successful match
} else {
// Match attempt failed

should do this for you.

^ anchors the search at the start of the string.

(?!\s*$), a so-called negative lookahead, asserts that it's impossible to match only whitespace characters until the end of the string.

.+ will then actually do the match. It will match anything (except newline) up to the end of the string. If you want to allow newlines, you'll have to set the RegexOptions.Singleline option.

Left over from the previous version of your question:


matches strings that contain only whitespace (or are empty).

The exact opposite:


matches only strings that consist of only non-whitespace characters, one character minimum.

Regex empty string or email

This regex pattern will match an empty string:


And this will match (crudely) an email or an empty string:


Make regular expression not match empty string?

Replace "*" with "+", as "*" means "0 or more occurrences", while "+" means "at least one occurrence"

Regex: match an empty string instead of nothing

You may solve the problem by replacing * after the (?:\[(?P<cap_2>[a-z]*)=?(?P<cap_3>[a-z]*)\])* repeated group with + and adding an alternative with a second occurrence of groups cap_2 and cap_3 (note that PyPi regex module supports multiple identically named groups in the same regex):

import regex as re
s = 'one.four'
pattern = re.compile(
^ # Assert start of string
(?P<cap1> # Start a new group for "one"
[a-z]* #
) #
(?: # Start a group for "two" and "three"
\[ # Match the "["
(?P<cap_2> # Start a group for "two"
[a-z]* #
) #
=? # Delimit two/three with "="
(?P<cap_3> # Start a group for "three"
[a-z]* #
) #
\] # Match the "]"
)+ # End the two-three group, allowing repeats
\.? # Delimit three/four with "."
(?P<cap_4> # Begin a group for "four"
[a-z]* #
) #
$ # Assert end of string

print ( pattern.match("one.four").captures("cap_2") )
# => ['']

See the Python demo

The thing is, the (?:\[(?P<cap_2>[a-z]*)=?(?P<cap_3>[a-z]*)\])* part matches by all means since it can match an empty string, and if you just add the alternatives without changing the modifier, the expected results won't be achieved. So, if there is no [...]s, the second cap_2 and cap_3 groups with empty patterns willmatch by all means capturing an empty string.

Regexp to match anything (including empty string) except character

You may use this grep:

grep -E '(^|[^_])key' file



  • -E: Enabled extended regex (ERE)
  • (^|[^_]): This part matches start of line of any character that is not _
  • key: match text key

If you have gnu-grep then you can use negative lookbehind with option -P (perl regex mode):

grep -P '(?<!_)key' file

What is a regex to match ONLY an empty string?

As explained in under the section "Strings Ending with a Line Break", \Z will generally match before the end of the last newline in strings that end in a newline. If you want to only match the end of the string, you need to use \z. The exception to this rule is Python.

In other words, to exclusively match an empty string, you need to use /\A\z/.

regex dont match if it is an empty string

You may use

/^(?! *$)[a-zA-Z.+ '-]+$/

Or - to match any whitespace


The (?!\s*$) negative lookahead will fail the match if the string is empty or only contains whitespace.

Pattern details

  • ^ - start of string
  • (?!\s*$) - no empty string or whitespaces only in the string
  • [a-zA-Z.+\s'-]+ - 1 or more letters, ., +, whitespace, ' or - chars
  • $ - end of string.

Note that actually, you may also use (?!\s+) lookahead here, since your pattern does not match an empty string due to the + quantifier at the end.

Regex to match anything (including the empty string) except a specific given string

More efficient than that large regex (depending, of course, on your data and the quality of the engine) is

WHERE col LIKE '%Kansas%' AND
(col NOT LIKE '%Kansas State%' OR
REPLACE(col, 'Kansas State', '') LIKE '%Kansas%')

If Kansas usually appears in the form 'Kansas State', though, you may find this better:

WHERE col LIKE '%Kansas%' AND
REPLACE(col, 'Kansas State', '') LIKE '%Kansas%'

This has the added advantage of being easier to maintain. It works less well if Kansas is common and text fields are large. Of course you can test these on your own data and tell us how they compare.

Regex for replacing anything other than characters, more than one spaces and number only in end with empty char


Related Topics

Leave a reply