# Stumped on simple wildcard

**URL:** <https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038>\
**Category:** Ruby\
**Created:** [August 16, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038 "2008-08-16T15:55:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![woodzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@woodzer](https://www.ruby-forum.com/u/woodzer)\
**Post date:** [August 16, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038/1 "2008-08-16T15:55:04Z")

</div>

I’m trying to use a wildcard in an if but the solution is evading me. Is  
there a special character ruby uses as a general wildcard? My problem  
below is our domains are all setup to resolve on any combination of  
[domain1.com](http://domain1.com) [www.domain1.com](http://www.domain1.com) [w2w.domain1.com](http://w2w.domain1.com) etc. It must also be case  
insensitive.

domain = ENV[‘HTTP\_HOST’]  
if domain == ‘\*[domain1.com](http://domain1.com)’; printf “

## Welcome to Domain 1
”  
elsif domain == ‘?[domain2.com](http://domain2.com)’; printf “
## Welcome to Domain 2
”  
elsif domain == ‘[domain3.com](http://domain3.com)’; printf “
## Welcome to Domain 3
”  
elsif domain == ‘[domain4.com](http://domain4.com)’; printf “
## Welcome to Domain 4
”  
elsif domain == ‘[domain5.com](http://domain5.com)’; printf “
## Welcome to Domain 5
”  
end

Any help is appreciated!!

Thanks

---

<div class="post-metadata">

**Author:** ![woodzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@woodzer](https://www.ruby-forum.com/u/woodzer)\
**Post date:** [August 16, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038/2 "2008-08-16T16:05:48Z")

</div>

SOLVED

---

<div class="post-metadata">

**Author:** ![woodzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@woodzer](https://www.ruby-forum.com/u/woodzer)\
**Post date:** [August 16, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038/3 "2008-08-16T16:12:40Z")

</div>

> domain = ENV[‘HTTP\_HOST’]  
> if domain == ‘\*[domain1.com](http://domain1.com)’; printf “
> 
> ## Welcome to Domain 1
> ”  
> elsif domain == ‘?[domain2.com](http://domain2.com)’; printf “
> ## Welcome to Domain 2
> ”  
> elsif domain == ‘[domain3.com](http://domain3.com)’; printf “
> ## Welcome to Domain 3
> ”  
> elsif domain == ‘[domain4.com](http://domain4.com)’; printf “
> ## Welcome to Domain 4
> ”  
> elsif domain == ‘[domain5.com](http://domain5.com)’; printf “
> ## Welcome to Domain 5
> ”  
> end

Hello. When you compare two strings using ==, it is the exact match and  
there are no wildcards in this kind of comparison. Use Regexen for your  
task.

Try this:

if mat=domain.match(/domain([0-9]).com$/i)  
dm=mat[1].to\_i  
if (1…5).include?(dm)  
printf “

## Welcome to Domain #{dm}
”  
else  
# bad domain number  
end  
else

# not matching the pattern

end

If you have domains with more digits (10, 11, …), change to ([0-9]+)  
in the Regex. Note the dot before com is escaped. The $ at the end means  
that the string must end at com (without it, [www.domain4.com.ru](http://www.domain4.com.ru) would  
qualify also). The /i at the end of the pattern makes it case  
insensitive. mat[1] reads the first parenthesised group from the Regexp,  
which is the domain number in our case.

Hope that helped.

---

<div class="post-metadata">

**Author:** ![woodzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f08c70/32.png) [@woodzer](https://www.ruby-forum.com/u/woodzer)\
**Post date:** [August 16, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/stumped-on-simple-wildcard/146038/4 "2008-08-16T16:27:16Z")

</div>

Thomas Bl. wrote:

> > domain = ENV[‘HTTP\_HOST’]  
> > if domain == ‘\*[domain1.com](http://domain1.com)’; printf “
> > 
> > ## Welcome to Domain 1
> > ”  
> > elsif domain == ‘?[domain2.com](http://domain2.com)’; printf “
> > ## Welcome to Domain 2
> > ”  
> > elsif domain == ‘[domain3.com](http://domain3.com)’; printf “
> > ## Welcome to Domain 3
> > ”  
> > elsif domain == ‘[domain4.com](http://domain4.com)’; printf “
> > ## Welcome to Domain 4
> > ”  
> > elsif domain == ‘[domain5.com](http://domain5.com)’; printf “
> > ## Welcome to Domain 5
> > ”  
> > end
> 
> Hello. When you compare two strings using ==, it is the exact match and  
> there are no wildcards in this kind of comparison. Use Regexen for your  
> task.
> 
> Try this:
> 
> if mat=domain.match(/domain([0-9]).com$/i)  
> dm=mat[1].to\_i  
> if (1…5).include?(dm)  
> printf “
> 
> ## Welcome to Domain #{dm}
> ”  
> else  
> # bad domain number  
> end  
> else
> 
> # not matching the pattern
> 
> end
> 
> If you have domains with more digits (10, 11, …), change to ([0-9]+)  
> in the Regex. Note the dot before com is escaped. The $ at the end means  
> that the string must end at com (without it, [www.domain4.com.ru](http://www.domain4.com.ru) would  
> qualify also). The /i at the end of the pattern makes it case  
> insensitive. mat[1] reads the first parenthesised group from the Regexp,  
> which is the domain number in our case.
> 
> Hope that helped.

Yes thanks.  
I did find a simpler way though. The ‘’ were the problem.

domain = ENV[‘HTTP\_HOST’]  
if domain =~ /domain1.com/;printf “

## Welcome to Domain 1
”  
elsif domain =~ /domain2.com/;printf “
## Welcome to Domain 2
”  
elsif domain =~ /domain3.com/;printf “
## Welcome to Domain 3
”  
elsif domain =~ /domain4.com/;printf “
## Welcome to Domain 4
”  
elsif domain =~ /domain5.com/;printf “
## Welcome to Domain 5
”  
end
