Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Stumped on simple wildcard
Peter Woodsky
8/16/2008 1:52:00 PM
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 www.domain1.com w2w.domain1.com etc. It must also be case
insensitive.
domain = ENV['HTTP_HOST']
if domain == '*domain1.com'; printf "<h2>Welcome to Domain 1</h2>"
elsif domain == '?domain2.com'; printf "<h2>Welcome to Domain 2</h2>"
elsif domain == 'domain3.com'; printf "<h2>Welcome to Domain 3</h2>"
elsif domain == 'domain4.com'; printf "<h2>Welcome to Domain 4</h2>"
elsif domain == 'domain5.com'; printf "<h2>Welcome to Domain 5</h2>"
end
Any help is appreciated!!
Thanks
--
Posted via
http://www.ruby-...
.
2 Answers
TPReal
8/16/2008 2:10:00 PM
0
> domain = ENV['HTTP_HOST']
> if domain == '*domain1.com'; printf "<h2>Welcome to Domain 1</h2>"
> elsif domain == '?domain2.com'; printf "<h2>Welcome to Domain 2</h2>"
> elsif domain == 'domain3.com'; printf "<h2>Welcome to Domain 3</h2>"
> elsif domain == 'domain4.com'; printf "<h2>Welcome to Domain 4</h2>"
> elsif domain == 'domain5.com'; printf "<h2>Welcome to Domain 5</h2>"
> 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 "<h2>Welcome to Domain #{dm}</h2>"
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 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.
--
Posted via
http://www.ruby-...
.
Peter Woodsky
8/16/2008 2:24:00 PM
0
Thomas Bl. wrote:
>> domain = ENV['HTTP_HOST']
>> if domain == '*domain1.com'; printf "<h2>Welcome to Domain 1</h2>"
>> elsif domain == '?domain2.com'; printf "<h2>Welcome to Domain 2</h2>"
>> elsif domain == 'domain3.com'; printf "<h2>Welcome to Domain 3</h2>"
>> elsif domain == 'domain4.com'; printf "<h2>Welcome to Domain 4</h2>"
>> elsif domain == 'domain5.com'; printf "<h2>Welcome to Domain 5</h2>"
>> 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 "<h2>Welcome to Domain #{dm}</h2>"
> 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 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 "<h2>Welcome to Domain 1</h2>"
elsif domain =~ /domain2.com/;printf "<h2>Welcome to Domain 2</h2>"
elsif domain =~ /domain3.com/;printf "<h2>Welcome to Domain 3</h2>"
elsif domain =~ /domain4.com/;printf "<h2>Welcome to Domain 4</h2>"
elsif domain =~ /domain5.com/;printf "<h2>Welcome to Domain 5</h2>"
end
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Stumped on simple wildcard
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password