...

View Full Version : REGEX and optional pattern...doesnt work



V@no.
05-13-2004, 06:26 PM
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!

bcarl314
05-13-2004, 06:37 PM
Hmm,

How about

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

bcarl314
05-13-2004, 06:42 PM
btw -

I put together a little regEx tester here:

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

V@no.
05-13-2004, 07:07 PM
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!

V@no.
05-13-2004, 07:09 PM
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 ;)

sidney
05-13-2004, 07:09 PM
a link to a nice regex help program regex coach

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

bcarl314
05-13-2004, 07:17 PM
How about this...

(match\d\w?)+

V@no.
05-13-2004, 07:24 PM
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 ...

bcarl314
05-13-2004, 07:40 PM
Sounds like back referencing.

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

sidney
05-13-2004, 07:45 PM
$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;

V@no.
05-13-2004, 08:02 PM
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. :o

maybe admins could move it to the correct forum? ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum