Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a regex to match empty string and a given word at the same time?

Tags:

regex

The value I'm trying to match to can either be a word OR it can be empty.

I need a regex that will do one of the following:

  1. Match the word 'default' or empty string

OR

  1. Match any non empty string that is not 'default'

The non empty string is important because "" is the same as default in my situation.

Is there a regex for this?

Anything that helps point me in the right direction is appreciated.

like image 832
kasdega Avatar asked Feb 22 '12 01:02

kasdega


People also ask

Does regex match empty string?

Java static code analysis: Repeated patterns in regular expressions should not match the empty string.

What does \b mean in regex?

The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Does empty regex match everything?

An empty regular expression matches everything.


1 Answers

Sure, the regex is pretty simple:

/^(|default)$/
like image 152
Lily Ballard Avatar answered Sep 28 '22 20:09

Lily Ballard