Regular Expression Help?

Discussion in 'Perl Misc' started by Trespasser, Feb 4, 2009.

  1. Trespasser

    Trespasser Guest

    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.
     
    Trespasser, Feb 4, 2009
    #1
    1. Advertising

  2. Trespasser

    Tim Greer Guest

    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!
     
    Tim Greer, Feb 4, 2009
    #2
    1. Advertising

  3. Trespasser

    Trespasser Guest

    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" <> wrote in message
    news:xA8il.1918$...
    > 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!
     
    Trespasser, Feb 4, 2009
    #3
  4. Trespasser <> 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/"
     
    Tad J McClellan, Feb 4, 2009
    #4
  5. "Trespasser" <> 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.
     
    Ben Bacarisse, Feb 4, 2009
    #5
  6. Trespasser

    Bruce Cook Guest

    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
     
    Bruce Cook, Feb 5, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,246
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    442
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,394
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    886
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
Loading...

Share This Page