Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question REGEX and optional pattern...doesnt work

    Hi!
    I know u guys sick and tired with questions about regex stuff, but I've wasted entire day yesterday trying solve this...
    I need it for mod_rewrite matching.

    I have a string:
    match1-match2a-match2b-match3a-match3b.htm
    All matches are separeted by " - ", but match2, match3, matchXX and first separator are optional and sequence could be unlimited.

    The regex that would match this example could be:
    ^(.+)(-(.*)-(.*))(-(.*)-(.*))(-(.*)-(.*)).htm
    works fine, but if the string has match4a-match4b or if string has only match2a-match2b it will not work...
    other words, I need a pattern that would match:
    match1.htm
    match1-match2a-match2b.htm
    match1-match2a-match2b-match3a-match3b.htm
    match1-match2a-match2b-match3a-match3b-match4a-match4b.htm
    and so on (unlimited).

    I have found many tutorials about mod_rewrite, but all of them are for specific number of matches, there is no "dynamic"...

    Can someone help me with that?

    Thanks!
    Last edited by V@no.; 05-13-2004 at 06:28 PM.

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Hmm,

    How about

    ^((.+?)-?)+.htm

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    btw -

    I put together a little regEx tester here:

    http://www.aphion.com/class/class11/regEx.php

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply!
    unfortunetly its not that easy
    with your pattern it returns entire string as one, I need separate each matchXX

    meaning I need get in the result this info from the string:

    1 - match1
    2 - match2
    3 - match3
    4 - match4

    and so on.

    thanks again!

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    btw, nice tester!

    wish it would "remmember" all previous expressions in a dropdown or something...
    or atleast leave the last one in the edit field

  • #6
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a link to a nice regex help program regex coach

    http://www.weitz.de/regex-coach/

  • #7
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    How about this...

    (match\d\w?)+

  • #8
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it still will match the whole string as one...
    I need cut it on pieces, to get separate everything that separated by " - "

    P.S. "match" word was just an example, it could be a word, it cold be a number...

    if this makes it easier, what I'm trying to do is change:
    web-page-2-some-query-blah-10.htm
    into:
    web.php?page=2&some=query&blah=10

    (number of queries in the address could be unlimited, that's the problem...)

    I know how to do that with mod_rewrite, but cant figure out the regex pattern ...

  • #9
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Sounds like back referencing.

    <loud voice="thunderous">
    I Hereby call the vast powers of Mordred
    </loud>

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $val='web-page-2-some-query-blah-10.htm';
    $val1=substr($val,0,-4);
    $joinarray=array(".php?","=","&");
    $array=explode("-",$val1);
    $count=0;
    $finish="";
    for(
    $i=0;$i<count($array);$i++)
        {
        if(
    $count==3){$count=1;}
        
    $finish.=$array[$i].$joinarray[$count];
        
    $count++;
        }
    $finish=substr($finish,0,-1);    
    echo 
    $finish

  • #11
    Regular Coder
    Join Date
    Feb 2003
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, crap!
    I'm sorry guys, I posted to the wrong forum...
    I should have posted it to "General web building"...I think...
    the regex pattern I requered is for mod_rewrite, witch will be used in .htaccess so no php could help there...
    sorry.

    maybe admins could move it to the correct forum?


  •  

    Posting Permissions

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