SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RewriteRule...just need to match any number of numbers and 2 periods

    Hi,

    I'm implementing mod_rewrite and need to rewrite URL's in the form:

    123.4567.89

    where the period could be anywhere in the string except the beginning or end, but there will always be 2. There can be any number of number digits.

    I thought that the following would work:

    ([0-9]+\.{2})

    but it doesn't. What might I be doing wrong? I can get

    ([0-9\.]+) to work, or even ([0-9\.{2}]+), but either of those match more than 2 periods.

    Thanks in advance.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    un,

    You got it! The reason a character range definition won't work for you is that it defines the set of characters for the entire range (as specified by the range modifier whethe it be ?, +, * or {x,y}. What you seem to want to do it capture three series of digits separated by two dot characters, isn't it? Doesn't that seem more like (\d+)\.(\d+)\.(\d+) ? Three sets of one or more digits separated by dot characters and each digital set is addressable by $1, $2 or $3. What more could you ask for ... once you can verbalize what you're trying to do?

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, dklynn, you are exactly right. That worked and was a more specific pattern match than I had initially described. Thanks! I'm new to mod_rewrite and regular expressions, but this has opened my eyes to a different way of thinking about them.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    un,

    Aw, I'd say it's all in the wrist but it's really how well you understand regular expressions. First, verbalize what you're trying to do (as I did above) then translate that to code in a regular expression.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •