Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg Ex help 2

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hi all.
I have the following 2 expressions
/.*?-reviews~([^~]*).*\.htm
/.*?-reviews~([^~]*)~([^~]*).*\.htm

These match up to the following strings

/sam-and-max-reviews~10.htm
and
/sam-and-max-reviews~10~PC.htm

These are then used in replaces using $1 $2 etc

Is there a way to combine the expressions

I thought that
/.*?-reviews(?:~([^~]*)){0,3}.*\.htm
May do it, but I only have access to the last match using the $1 group

Any ideas?


}...the bane of my life!
 
I'm not good at these things, so the best I'll do is try, it'll help me learn. I'm trying to show that you would want the empty string OR the ~([^~]*) part.

Code:
/.*?-reviews~([^~]*)[!](()|~([^~]*))[/!].*\.htm



<.

 
Can you show a few examples of what you got returned that you DIDN'T want returned from my example??

<.

 
I got a new one, not as cryptic since you only need those 2 to match:

Code:
/.*?-reviews~10(()|~PC).htm

<.

 
Like so?
[tt]
dim rx,s,a

set rx=new regexp
with rx
.pattern="^/.*?-reviews((~[^~]*?)+)\.htm$"
.ignorecase=true
'etc etc if needed
end with

s="/sam-and-max-reviews~10~PC~abc~xyz.htm"

if rx.test(s) then
a=split(rx.execute(s)(0).submatches(0),"~")
'a(0) blank, a(1)=10 etc
'monitor the ubound(a) if need restriction
'or replace the "+" by {1,n}
'other manipulations according to need
end if
[/tt]
Slight modifications if allowing no "~..~...." suffix.
 
Both interesting, second one should work...but the reason i need to do this with matched groups is that I am not able to use a nice loop, I am using isapi_rewrite, i posted here as I know you guys are good at the reg exp!!

I tried this
/.*?-reviews~([^~]*)(?(~)~([^~]*)).*\.htm$
Which is nice but i need a similar pattern for the match.
I think you both deserve stars for the help!!

}...the bane of my life!
 
I don't understand exactly the problem. But if you think you have to make good use of $1,...$n, the following shows you how to determine the matching n and each value of them ($1,...$n). The speed of it may not be the best of the world, but it shows how to proceed.
[tt]
set rx=new regexp
with rx
.ignorecase=true
'etc etc if needed
end with

s="/sam-and-max-reviews~10~PC~abc~xyz.htm"

n=1
nmax=100 'n limit to reasonably big according to need
bfound=false

do while (not bfound) and (n<nmax)
spattern="^/.*?-reviews"
for i=0 to n-1
spattern=spattern & "(~[^~]*?)"
next
spattern=spattern & "\.htm$"

rx.pattern=spattern

if rx.test(s) then
bfound=true
response.write "last-n=" & n & "<br />" & "$last-n=" & rx.replace(s,"$" & n) & "<br />"
if n>1 then
response.write "$last-but-one=" & rx.replace(s,"$" & (n-1)) & "<br />"
end if
if n>2 then
response.write "$1$2=" & rx.replace(s,"$1$2") & "<br />"
end if
else
n=n+1
end if
loop
if not bfound then 'or n=nmax at this level
response.write "no match found" & "<br />"
end if
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top