Regular Expression Help?

  • Follow


Hi,

I can appreciate what a wonderfully powerful tool regular expressions are, 
but I am still learning and seem to need a little help.

I have an example of a regular expression for an Apache server mod_rewrite 
module rewrite trigger condition.

^www.\.[^.]+\.domain\.com$

As far as I can gather this is supposed to match www.username.domain.com, 
where 'username' could be any system user's username or other alphanumeric 
character sequence.

Can anyone please tell me how I can modify this type of regular expression 
as follows? I want the rule to match for sequences like username.domain.com 
and otherusername.domain.com, but specifically not to match for 
www.domain.com.

I would so appreciate it if somebody could please lend me some of their 
valuable expertise to help solve my dilemma.

Regards,
Tressie.




0
Reply valid (45) 2/4/2009 4:03:07 AM

Trespasser wrote:

> Hi,
> 
> I can appreciate what a wonderfully powerful tool regular expressions
> are, but I am still learning and seem to need a little help.
> 
> I have an example of a regular expression for an Apache server
> mod_rewrite module rewrite trigger condition.
> 
> ^www.\.[^.]+\.domain\.com$

You probably don't want ^www.\., but ^www\. instead.

> As far as I can gather this is supposed to match
> www.username.domain.com, where 'username' could be any system user's
> username or other alphanumeric character sequence.
> 
> Can anyone please tell me how I can modify this type of regular
> expression as follows? I want the rule to match for sequences like
> username.domain.com and otherusername.domain.com, but specifically not
> to match for www.domain.com.

What about domain.com?  Will you want to be matching
www.something.example.com, still as well?
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
0
Reply Tim 2/4/2009 4:12:12 AM


Hi Tim,

Thanks very much for responding to my regular expression query.

^www\..[^.]+\.domain\.com$

Yes, this is the corrected form of the example expression. Sorry about the 
mix up.

> What about domain.com?  Will you want to be matching
> www.something.example.com, still as well?

I've just discovered that I can create DNS A records from URLs of the 
www.something.example.com form. This form of match is interesting as well. I 
think the example trigger expression is intended to accommodate this form, 
but not the www.domain.com and username.domain.com forms as well.

Yes, triggering on username.domain.com and otherusername.domain.com, but not 
on www.domain.com is the single focus my interest here. I can't figure how 
to write a regular expression that triggers on the 3-tier URL, but not if 
the first word is www.

Any suggestions, Tim?

Regards,
Tressie.

"Tim Greer" <tim@burlyhost.com> wrote in message 
news:xA8il.1918$ml6.1098@newsfe09.iad...
> Trespasser wrote:
>
>> Hi,
>>
>> I can appreciate what a wonderfully powerful tool regular expressions
>> are, but I am still learning and seem to need a little help.
>>
>> I have an example of a regular expression for an Apache server
>> mod_rewrite module rewrite trigger condition.
>>
>> ^www.\.[^.]+\.domain\.com$
>
> You probably don't want ^www.\., but ^www\. instead.
>
>> As far as I can gather this is supposed to match
>> www.username.domain.com, where 'username' could be any system user's
>> username or other alphanumeric character sequence.
>>
>> Can anyone please tell me how I can modify this type of regular
>> expression as follows? I want the rule to match for sequences like
>> username.domain.com and otherusername.domain.com, but specifically not
>> to match for www.domain.com.
>
> What about domain.com?  Will you want to be matching
> www.something.example.com, still as well?
> -- 
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle! 


0
Reply Trespasser 2/4/2009 9:52:28 AM

Trespasser <valid@email.address> wrote:
> Hi Tim,
>
> Thanks very much for responding to my regular expression query.
>
> ^www\..[^.]+\.domain\.com$
>
> Yes, this is the corrected form of the example expression. 


That will match

    www..user.domain.com

....


> Yes, triggering on username.domain.com and otherusername.domain.com, but not 
> on www.domain.com is the single focus my interest here. I can't figure how 
> to write a regular expression that triggers on the 3-tier URL, but not if 
> the first word is www.
>
> Any suggestions, Tim?


I am not Tim, yet I have a suggestion.

Use a negative lookahead assertion:

-----------------------------
#!/usr/bin/perl
use warnings;
use strict;

foreach my $domain ( qw/ www.domain.com www.something.domain.com
                         username.domain.com otherusername.domain.com
                         wwwfoobar.domain.com / ) {
    print "$domain matched\n"
        if $domain =~ /^(?!www\.)[^.]+\.domain\.com$/;
}
-----------------------------


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
0
Reply Tad 2/4/2009 12:24:21 PM

"Trespasser" <valid@email.address> writes:

> Thanks very much for responding to my regular expression query.
>
> ^www\..[^.]+\.domain\.com$
>
> Yes, this is the corrected form of the example expression. Sorry about the 
> mix up.

Tim suggested /^www\.[^.]+\.domain\.com$/.  You still have an extra
'.' in there.

>> What about domain.com?  Will you want to be matching
>> www.something.example.com, still as well?
>
> I've just discovered that I can create DNS A records from URLs of the 
> www.something.example.com form. This form of match is interesting as well. I 
> think the example trigger expression is intended to accommodate this form, 
> but not the www.domain.com and username.domain.com forms as well.
>
> Yes, triggering on username.domain.com and otherusername.domain.com, but not 
> on www.domain.com is the single focus my interest here. I can't figure how 
> to write a regular expression that triggers on the 3-tier URL, but not if 
> the first word is www.

Perl has a negative look-behind that can be used when the string you
must avoid is fixed length:

   /(?<!www)\.domain\.com$/

but this may not be what you want.  Note the ^ has gone.  This meets
the narrow interpretation of your request but may not do what want
with names like www.other.domain.com (matches) and
other.www.domain.com (it does not match).  I think that was the force
of Tim's clarifying question.

-- 
Ben.
0
Reply Ben 2/4/2009 2:05:06 PM

Trespasser wrote:

> Hi,
> 
> I can appreciate what a wonderfully powerful tool regular expressions are,
> but I am still learning and seem to need a little help.
> 
> I have an example of a regular expression for an Apache server mod_rewrite
> module rewrite trigger condition.
> 
> ^www.\.[^.]+\.domain\.com$

[...]

I'm not sure if you've found this already, but you can turn on logging for 
mod_rewrite in apache.  I have found it quite useful in the past.

Bruce


0
Reply Bruce 2/5/2009 10:02:39 AM

5 Replies
18 Views

(page loaded in 0.093 seconds)

Similiar Articles:













7/23/2012 4:41:40 AM


Reply: