Regular Expression Which Matches a Pattern, or Is an Empty String

Regex empty string or email

This regex pattern will match an empty string:

^$

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

(^$|^.*@.*\..*$)

What is a regex to match ONLY an empty string?

As explained in http://www.regular-expressions.info/anchors.html 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/.

Regular expression which matches a pattern, or is an empty string

To match pattern or an empty string, use

^$|pattern

Explanation

  • ^ and $ are the beginning and end of the string anchors respectively.
  • | is used to denote alternates, e.g. this|that.

References

  • regular-expressions.info/Anchors and Alternation

On \b

\b in most flavor is a "word boundary" anchor. It is a zero-width match, i.e. an empty string, but it only matches those strings at very specific places, namely at the boundaries of a word.

That is, \b is located:

  • Between consecutive \w and \W (either order):

    • i.e. between a word character and a non-word character
  • Between ^ and \w

    • i.e. at the beginning of the string if it starts with \w
  • Between \w and $

    • i.e. at the end of the string if it ends with \w

References

  • regular-expressions.info/Word Boundaries

On using regex to match e-mail addresses

This is not trivial depending on specification.

Related questions

  • What is the best regular expression for validating email addresses?
  • Regexp recognition of email address hard?
  • How far should one take e-mail address validation?

Matching an empty string with regex

^\s+$ - does that not also work?

Not for matching an empty string. In general, X+ means X one or more times. So, \s+ cannot match the empty string - it requires at least one \s in order to match.


^ \s + $
| | | |
start of string ---------------------+ | | |
whitespace character ------------------+ | |
one or more of what precedes -------------+ |
end of string ------------------------------+

Now, X* means X 0 or more times, so ^\s*$ would indeed match an empty string.


^\s+$

Sample Image

^\s*$

Sample Image

regex dont match if it is an empty string

You may use

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

Or - to match any whitespace

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

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.

regular expression for anything but an empty string

^(?!\s*$).+

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

So

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:

^\s*$

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

The exact opposite:

^\S+$

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

Why empty string is added in the output array while matching a string using regex pattern in javascript?

The *, or Kleene Star, in regular expressions means "zero or more", so it matches empty string.

The + means "one or more" instead, so it does not match empty string.


Given that we have as follows

a = 'bb bbbb bbb';

Let's represent it like this:

a = /^bb bbbb bbb$/;

where ^ is start of string and $ is end of string.

Our pattern is (bb)*. This means that the engine will look for a sequence of two b "zero or more times". This means the pattern matches bb if the sequence is found or empty string otherwise.

Now step by step I'll use a dot (.) to represent regexp analysis.

STEPS

        STRING              MATCH ARRAY
start - /^.bb bbbb bbb$/ -> [ ]
1 - /^bb. bbbb bbb$/ -> [ "bb" ]
2 - /^bb .bbbb bbb$/ -> [ "bb", "" ]
3 - /^bb bbbb. bbb$/ -> [ "bb", "", "bbbb" ]
4 - /^bb bbbb .bbb$/ -> [ "bb", "", "bbbb", "", ]
5 - /^bb bbbb bb.b$/ -> [ "bb", "", "bbbb", "", "bb" ]
6 - /^bb bbbb bbb.$/ -> [ "bb", "", "bbbb", "", "bb", "", ]
7 - /^bb bbbb bbb$./ -> [ "bb", "", "bbbb", "", "bb", "", "" ]

In STEP 1 we have pushed bb since the pattern matched one sequence.

In STEP 2 we have pushed "" because the pattern matched empty string.

In STEP 3 we have pushed bbbb because the pattern matched two sequences.

STEP 4 -> STEP 2

STEP 5 -> STEP 1

In STEP 6 we have pushed "" because the pattern matched empty string since, only one b was found.

Finally you encounter $ token and it matches empty string again.

Note that if the string was "bb bbbb bbbb" the array would have been ["bb", "", "bbbb", "", "bbbb", ""]

Regex to match a pattern or empty value

Overall you could use the pattern expression|$, so it will try to match the expression or (|) the empty , and we make sure we don't have anything after that including the anchor $.
Furthermore, we could enclose it with a capture group (...), so it will finally look like this:

ˆ(expresison|)$

So applying it to your need, it would end up to be like:

^([0-9a-z]{4}|)$

here is an example

EDIT:

If you want to match also uppercases, add A-Z to the pattern:

^([0-9a-zA-Z]{4}|)$

Java Regex,Empty String be matched

Your regex pattern looks like

((^|([.!?:] ))[^!.?:]*?Hello.*?([!.?])|$)+?

It contains 2 alternatives:

  1. (^|([.!?:] ))[^!.?:]*?Hello.*?([!.?])
  2. $

So, the problem was that you were matching the end of string all the time in a loop.

Make this change:

String regex = "(^|[.!?:] )" + "[^!.?:]*?" + queryStr + ".*?" + "([!.?]+?|$)";

Now, it will look like

(^|[.!?:] )[^!.?:]*?Hello.*?([!.?]+?|$)

And $ will be an alternative to [!.?]+? only.

Sample Image

See demo on regex101.com.



Related Topics



Leave a reply



Submit