# Regexp which matches nothing?

**URL:** <https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482>\
**Category:** Ruby\
**Created:** [August 27, 2009, 11:57am UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482 "2009-08-27T11:57:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 11:57am UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/1 "2009-08-27T11:57:56Z")

</div>

How i can construct regexp, which will always matches nothing?

For example:

“xxxxxxx”.match /my\_special\_regexp/  
=\> should be ALWAYS nil

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/2 "2009-08-27T12:05:48Z")

</div>

Joao S. wrote:

> How i can construct regexp, which will always matches nothing?
> 
> For example:
> 
> “xxxxxxx”.match /my\_special\_regexp/  
> =\> should be ALWAYS nil

How about:

re = /$a^/

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/3 "2009-08-27T12:06:43Z")

</div>

Why do you want to have a such regex ?

Anyway, it seems that /^$a/ will never match with any string.

“a”.match /^$a/  
=\> nil  
“”.match /^$a/  
=\> nil  
"a ".match /^$a/  
=\> nil

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:13pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/4 "2009-08-27T12:13:40Z")

</div>

7stud – wrote:

> Joao S. wrote:
> 
> > How i can construct regexp, which will always matches nothing?
> > 
> > For example:
> > 
> > “xxxxxxx”.match /my\_special\_regexp/  
> > =\> should be ALWAYS nil
> 
> How about:
> 
> re = /$a^/

I guess that could be made even simpler:

re = /a^/

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/5 "2009-08-27T12:23:17Z")

</div>

Joao S. wrote:

> How i can construct regexp, which will always matches nothing?
> 
> For example:
> 
> “xxxxxxx”.match /my\_special\_regexp/  
> =\> should be ALWAYS nil  
> not sure, but something that should always be nil … what about the  
> constant ‘nil’ 😉

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/6 "2009-08-27T12:25:37Z")

</div>

Joao S. wrote:

> How i can construct regexp, which will always matches nothing?
> 
> For example:
> 
> “xxxxxxx”.match /my\_special\_regexp/  
> =\> should be ALWAYS nil

Just out of curiosity: Why do you use a method call, when you expect a  
constant?

---

<div class="post-metadata">

**Author:** ![yankees26](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/eb9ed0/32.png) [@yankees26](https://www.ruby-forum.com/u/yankees26)\
**Post date:** [August 27, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/regexp-which-matches-nothing/174482/7 "2009-08-27T12:15:45Z")

</div>

7stud – wrote:

> 7stud – wrote:
> 
> > Joao S. wrote:
> > 
> > > How i can construct regexp, which will always matches nothing?
> > > 
> > > For example:
> > > 
> > > “xxxxxxx”.match /my\_special\_regexp/  
> > > =\> should be ALWAYS nil
> > 
> > How about:
> > 
> > re = /$a^/
> 
> I guess that could be made even simpler:
> 
> re = /a^/

…and probably more efficient:

re = /a\A/
