Home > Archive > ASP > March 2008 > Regex replace
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Guoqi Zheng 2008-03-17, 6:57 pm |
| Dear Sir, I need to use regex to replace some string.
Below is what I use.
output = "sample data <href=""xlink:GG44-33"">, part two
<href=""xlink:GG55-123"">"
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "href=\""(xlink:.*?)\"""
output = regEx.Replace(output,"href="&chr(34) &
GetDatabaseLink("$1")&chr(34))
Function GetDatabaseLink(Byval input)
'' query database based on "input" value and return value on database.
End Function
the value inside <href> tag needs to be replaced by real value from
database. Function GetDatabaseLink get the value from match string and send
it to database for query. What the database receive right now is the value
of "$1", instead of "xlink:GG44-33" and "xlink:GG55-123" in this case.
What did I do wrong?
Regards,
Guoqi Zheng
| |
| Anthony Jones 2008-03-18, 7:56 am |
|
"Guoqi Zheng" <no@sorry.com> wrote in message
news:235D4433-6744-44F8-A41B-450D89D45D32@microsoft.com...
> Dear Sir, I need to use regex to replace some string.
>
> Below is what I use.
>
> output = "sample data <href=""xlink:GG44-33"">, part two
<href=""xlink:GG55-123"">"
> Dim regEx
> Set regEx = New RegExp
> regEx.Pattern = "href=\""(xlink:.*?)\"""
> output = regEx.Replace(output,"href="&chr(34) &
> GetDatabaseLink("$1")&chr(34))
>
> Function GetDatabaseLink(Byval input)
> '' query database based on "input" value and return value on database.
> End Function
>
> the value inside <href> tag needs to be replaced by real value from
> database. Function GetDatabaseLink get the value from match string and
send
> it to database for query. What the database receive right now is the value
> of "$1", instead of "xlink:GG44-33" and "xlink:GG55-123" in this case.
>
> What did I do wrong?
RegEx will replace any $1 it finds in the string passed to it. The
expression:-
"href="&chr(34) & GetDatabaseLink("$1")&chr(34)
is evaluated first then its result passed into the the Replace method.
Therefore GetDataBaseLink("$1") executes as is with no replacement of $1.
First thing I would do is lose the extraneous href= etc from around the
pattern. Use:-
regEx.Pattern = """xlink:[^""]*"
regEx.Global = True
output = regEx.Replace(output, GetRef("ReplaceXLink"))
Function ReplaceXLink(rsXLink, vlOffset, rsSource)
ReplaceXLink = """" & GetDatabaseLink(Mid(rsXLink, 2, Len(rsXink) -2)) &
""""
End Function
This will globally replace all xlinks in the string with the appropriate
value looked up from the DB.
--
Anthony Jones - MVP ASP/ASP.NET
|
|
|
|
|