SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    130
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Regex: matching a pattern that may repeat x times

    Hi, i'm curious. How can i modify this expresssion so that it matches not only "alphanum-alphanum" but also "alphanum-alphanum-alphanum-alphanum" or any other number of repetitions of the pattern?

    Here's the expression:
    Code:
    $test = preg_match("/^[a-z0-9]+-*[a-z0-9]+$/i", $str);

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match('/^([a-z0-9]+-[a-z0-9]+)*$/im'$subject$match);
    print 
    '<pre>' print_r($match,1) . '</pre>'
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rather

    '/^([a-z0-9]+(?:-[a-z0-9]+)+)$/im'

    or if you want to match it exactly four times:

    '/^([a-z0-9]+(?:-[a-z0-9]+){3})$/im'
    Last edited by kleineme; Jul 12, 2007 at 02:22. Reason: forgot the "+" after the [a-z0-9]s
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


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
  •