DidiahProd
asked on
Regex: Replace and parse code within custom tags
I have a number of long strings in a database that are interspersed with custom tags example:
texttexttexttext[%im 111023%]... text text text[%im 111010%]text text text.
i need some code that will give me the ability to build a string that looks like:
texttexttext <img src="EVALUATEDCODE(111023) " /> text text <img src="EVALUATEDCODE(111010) " />text text text
texttexttexttext[%im 111023%]... text text text[%im 111010%]text text text.
i need some code that will give me the ability to build a string that looks like:
texttexttext <img src="EVALUATEDCODE(111023)
Actually, this is probably more like it....
Dim InputString As String = "texttexttexttext[%im 111023%]... text text text[%im 111010%]text text text."
Dim rx As New System.Text.RegularExpressions.Regex("(\[%im )(.*?)(%\])")
MsgBox(rx.Replace(InputString, "<img src=""EVALUATEDCODE($2)"" />"))
webtubbs has it, but if you want an alternate pattern:
match on:
\[%im\x20(\d+)%\]
replace with:
<img src="EVALUATEDCODE($1)" />
match on:
\[%im\x20(\d+)%\]
replace with:
<img src="EVALUATEDCODE($1)" />
If you actually want to call some function to generate an image path for the custom tag matches, check out the code below. You would call ParseText and send it your string from the database.
Let me know if you need it in VB instead of C#.
Let me know if you need it in VB instead of C#.
using System.Text.RegularExpressions;
...
public string ParseText(string inputText) {
return Regex.Replace(inputText, @"\[%im (\d+)%\]", new MatchEvaluator(EvaluateTagMatch), RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
private string EvaluateTagMatch(Match match) {
// Get the number (maybe it's an image ID) from the match.
int ImageId = int.Parse(match.Groups[1].Value);
// Evaluate some code with the number as the parameter to get an image source path.
string ImagePath = GetImagePath(ImageId);
// Return the custom tag replacement in full.
return string.Format(@"<img src=""{0}"" />", ImagePath);
}
ASKER
i got webtubbs to work as follows- is there a benefit to ventaur's solution? (I do need vb insead of C#).
Thanks!
Thanks!
Private Function CREval(ByVal strToEval)
Return "HI" & strToEval & "Bye"
End Function
Public Function GetMainContent(ByVal mstr As String) As String
Dim rx As New System.Text.RegularExpressions.Regex("(\[%im)(.*?)(%\])")
Return (rx.Replace(mstr, CREval("$2")))
End Function
The one benefit to the pattern that ventaur used (essentially identical to mine) is that is only utilizes one capture group while webtubbs used a pattern that contained three capture groups, two more than required. Other than that, effectively it will work the same.
ASKER
ventaur, could you post a vb version of that?
I must have misunderstood your use with "EVALUATEDCODE"; thus, the implementation using a match evaluator. But, yes the pattern is the same as ddrudik's and he should get credit if the rest of your code works.
Otherwise, here's the VB of mine using the match evaluator.
Otherwise, here's the VB of mine using the match evaluator.
Imports System.Text.RegularExpressions
...
Public ParseText(inputText As String) As String
Return Regex.Replace(inputText, "\[%im (\d+)%\]", new MatchEvaluator(EvaluateTagMatch), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
End Function
Private EvaluateTagMatch(match As Match) As String
' Get the number (maybe it's an image ID) from the match.
Dim ImageId As Integer = Integer.Parse(match.Groups[1].Value)
' Evaluate some code with the number as the parameter to get an image source path.
Dim ImagePath As String = GetImagePath(ImageId)
' Return the custom tag replacement in full.
Return String.Format("<img src=""{0}"" />", ImagePath)
End Function
ASKER
Thanks! Looks like we're on the right track.
This code doesn't seem to work, however. I get the error "Error 37 'System.Text.RegularExpres sions.Matc hEvaluator ' is a delegate type and requires a single 'addressof' expression as the only argument to the constructor. D:\inetpub\vhosts\jhcr\dev \SitePage. aspx.vb 10 78 D:\...\dev\
This code doesn't seem to work, however. I get the error "Error 37 'System.Text.RegularExpres
Sorry about that. I converted without the IDE.
Public ParseText(inputText As String) As String
Return Regex.Replace(inputText, "\[%im (\d+)%\]", new MatchEvaluator(AddressOf(EvaluateTagMatch)), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
End Function
ASKER
hmmm- we're close!
now i get this error
Compiler Error Message: BC30455: Argument not specified for parameter 'match' of 'Private Function EvaluateTagMatch(match As System.Text.RegularExpress ions.Match ) As String'.
now i get this error
Compiler Error Message: BC30455: Argument not specified for parameter 'match' of 'Private Function EvaluateTagMatch(match As System.Text.RegularExpress
Private Function EvaluateTagMatch(ByVal match As Match) As String
' Get the number (maybe it's an image ID) from the match.
Dim ImageId As Integer = Integer.Parse(match.Groups(1).Value)
' Evaluate some code with the number as the parameter to get an image source path.
Dim ImagePath As String = ImageId.ToString
' Return the custom tag replacement in full.
Return String.Format("<img src=""{0}"" />", ImagePath)
End Function
Public Function GetMainContent(ByVal inputText As String) As String
Return Regex.Replace(inputText, "\[%show (\d+)%\]", New MatchEvaluator(AddressOf (EvaluateTagMatch())), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
end function
Make sure you do not use parentheses after "EvaluateTagMatch" or you'll end up actually calling that function instead of passing the address of it to the match evaluator.
Return Regex.Replace(inputText, "\[%show (\d+)%\]", New MatchEvaluator(AddressOf (EvaluateTagMatch)), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
ASKER
I'm sorry i'm not following- EvaluateTagMatch requires a match as a parameter- this function doesn't include one.
i get that as anerror as well
Thanks
JEd
i get that as anerror as well
Thanks
JEd
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! vb/csharp can be headache
Try this...
Dim InputString As String = "texttexttexttext[%im 111023%]... text text text[%im 111010%]text text text."
Dim rx As New System.Text.RegularExpress
MsgBox(rx.Replace(InputStr
Regards,
Wayne