Avatar billede benkebus Nybegynder
12. november 2003 - 09:56 Der er 13 kommentarer og
1 løsning

Search and Replace with Regular Expressions

I have a string that contains text and custom tags for pictures. the custom tag looks like this: <picture_x#al>

is it possible via regexp to replace the custom tags with ordinary imagetags where src=picture_x and align=al? there will be no more than 5 custom tags per string and the imagenames will be img1, img2 and so on.
Avatar billede medions Nybegynder
12. november 2003 - 10:10 #1
You don't have to use Reg. Exp. for this... You can use this function insted:

Function ReplaceThis(strTxt)
  arrAlign = Split(arrAlign, "#")
  strTxt = Replace(strTxt, "<picture_x#al>", "<img src=""" & arrAlign(LBound(arrAlign) & """ align=""" & arrAlign(UBound(arrAlign) & """ >")
End Function

Response.Write(ReplaceThis("test<picture_x#al>test")

//>Rune
Avatar billede benkebus Nybegynder
12. november 2003 - 11:54 #2
Error Type:
Kompileringsfel i Microsoft VBScript (0x800A03EE)
')' förväntas.
/axelsons/nyhetstest.asp, line 7, column 138
strTxt = Replace(strTxt, "<picture_x#al>", "<img src=""" & arrAlign(LBound(arrAlign) & """ align=""" & arrAlign(UBound(arrAlign) & """ >")
-----------------------------------------------------------------------------------------------------------------------------------------^
Avatar billede medions Nybegynder
12. november 2003 - 12:00 #3
Function ReplaceThis(strTxt)
  arrAlign = Split(arrAlign, "#")
  strTxt = Replace(strTxt, "<picture_x#al>", "<img src='" & arrAlign(LBound(arrAlign) & " align='" & arrAlign(UBound(arrAlign) & "'>")
End Function

Response.Write(ReplaceThis("test<picture_x#al>test")
Avatar billede benkebus Nybegynder
12. november 2003 - 12:02 #4
Error Type:
Kompileringsfel i Microsoft VBScript (0x800A03EE)
')' förväntas.
/axelsons/nyhetstest.asp, line 7, column 132
strTxt = Replace(strTxt, "<picture_x#al>", "<img src='" & arrAlign(LBound(arrAlign) & " align='" & arrAlign(UBound(arrAlign) & "'>")
-----------------------------------------------------------------------------------------------------------------------------------^
Avatar billede medions Nybegynder
12. november 2003 - 12:06 #5
Hmm måske:

Function ReplaceThis(strTxt)
  arrAlign = Split(arrAlign, "#")
  strReplace = "<img src='" & arrAlign(LBound(arrAlign) & " align='" & arrAlign(UBound(arrAlign) & "'>"
  strTxt = Replace(strTxt, "<picture_x#al>", strReplace)
End Function

Response.Write(ReplaceThis("test<picture_x#al>test")

//>Rune
Avatar billede benkebus Nybegynder
12. november 2003 - 12:36 #6
Error Type:
Kompileringsfel i Microsoft VBScript (0x800A03EE)
')' förväntas.
/axelsons/nyhetstest.asp, line 7, column 101
strReplace = "<img src='" & arrAlign(LBound(arrAlign) & " align='" & arrAlign(UBound(arrAlign) & "'>"
----------------------------------------------------------------------------------------------------^
Avatar billede medions Nybegynder
12. november 2003 - 12:40 #7
strReplace = "<img src='" & arrAlign(LBound(arrAlign)) & "' align='" & arrAlign(UBound(arrAlign)) & "'>"

//>Rune
Avatar billede benkebus Nybegynder
12. november 2003 - 12:43 #8
am i supposed to run this function 5 times if i have 5 custom tags with img1, img2, img3, img4 and img5?

thats why i thought regExo should be the solution...
Avatar billede benkebus Nybegynder
12. november 2003 - 12:46 #9
i'm supposed to use this in a news managementsystem... in every article there can be up to 5 images in custom tags mixed in with the articletext like this: word word word word word word word word <bild1#left> word word word word word word word word word word <bild2#right>word word word word word word word word <bild3#left>word word word word word word word word <bild4#center>word word word word word word  <bild5#right>.
Avatar billede eagleeye Praktikant
12. november 2003 - 23:19 #10
benkebus your right, the solution would be a regular expressition to do the replace as it support variables, which is needed to find the number and align text.

Try this, it will replace <bildX#vvv> with <img src="picture_X.jpg" align="vvv">

<%

str = "i'm supposed to use this in a news managementsystem... in every article there can be up to 5 images in custom tags mixed in with the articletext like this: word word word word word word word word <bild1#left> word word word word word word word word word word <bild2#right>word word word word word word word word <bild3#left>word word word word word word word word <bild4#center>word word word word word word  <bild5#right>."
response.Write "Test String:<br>" & str


Set regEx = New RegExp
regEx.Global = true
regEx.IgnoreCase = true
regEx.Pattern = "<bild(\d)#([a-z]*)>"
RegEx.Global = true
str = RegEx.Replace(str,"<img src=""picture_$1.jpg"" align=""$2"">")
Set RegEx = Nothing

Response.Write "<br><br>Test Result:<br>"
Response.Write str
%>
Avatar billede benkebus Nybegynder
13. november 2003 - 07:23 #11
That worked fine... if i'd like to reverse the process and replace the <img src="picture_x.jpg" align="align"> back to the original custom tag, how would the regEx.pattern look like then?
Avatar billede benkebus Nybegynder
13. november 2003 - 14:46 #12
help...
if "$2"="center" then
str = RegEx.Replace(str,"<div align=""$2""><img src=""bilder/upload/bild$1.jpg""></div>")
else
str = RegEx.Replace(str,"<img src=""bilder/upload/bild$1.jpg"" align=""$2"">")
end if

this doesn't work... how should i do it... if a image is centred in the customtag it must be written with <div align=></div>?????
Avatar billede eagleeye Praktikant
13. november 2003 - 18:57 #13
if i'd like to reverse the process and replace the <img src="picture_x.jpg" align="align"> back to the original custom tag, how would the regEx.pattern look like then?


regEx.Pattern = "<img src=""picture_(\d).jpg"" align=""([a-z])*"">"
RegEx.Global = true
str = RegEx.Replace(str,"<bild$1#$2>")
Avatar billede benkebus Nybegynder
14. november 2003 - 07:17 #14
help...
if "$2"="center" then
str = RegEx.Replace(str,"<div align=""$2""><img src=""bilder/upload/bild$1.jpg""></div>")
else
str = RegEx.Replace(str,"<img src=""bilder/upload/bild$1.jpg"" align=""$2"">")
end if

this doesn't work... how should i do it... if a image is centred in the customtag it must be written with <div align=></div>?????
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester