• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 738
  • Last Modified:

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
0
DidiahProd
Asked:
DidiahProd
  • 6
  • 5
  • 2
  • +1
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Hi DidiahProd,

Try this...

        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"" />"))

Regards,

Wayne
0
 
Wayne Taylor (webtubbs)Commented:
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)"" />"))

Open in new window

0
 
ddrudikCommented:
webtubbs has it, but if you want an alternate pattern:
match on:
\[%im\x20(\d+)%\]
replace with:
<img src="EVALUATEDCODE($1)" />
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
ventaurCommented:
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#.
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);
}

Open in new window

0
 
DidiahProdAuthor Commented:
i got webtubbs to work as follows-   is there a benefit to ventaur's solution?  (I do need vb insead of C#).
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

Open in new window

0
 
ddrudikCommented:
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.
0
 
DidiahProdAuthor Commented:
ventaur, could you post a vb version of that?
0
 
ventaurCommented:
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.
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

Open in new window

0
 
DidiahProdAuthor Commented:
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.RegularExpressions.MatchEvaluator' 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\
0
 
ventaurCommented:
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

Open in new window

0
 
DidiahProdAuthor Commented:
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.RegularExpressions.Match) As String'.

    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

Open in new window

0
 
ventaurCommented:
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)

Open in new window

0
 
DidiahProdAuthor Commented:
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
0
 
ventaurCommented:
No, I'm sorry. I'm just not with it today. I finally got to a location that had Visual Studio so I can refresh my memory on VB syntax. The AddressOf statement is not a function, so please use the code below to avoid all errors this time.
Return Regex.Replace(inputText, "\[%show (\d+)%\]", New MatchEvaluator(AddressOf EvaluateTagMatch), RegexOptions.IgnoreCase Or RegexOptions.Multiline)

Open in new window

0
 
DidiahProdAuthor Commented:
Thanks! vb/csharp can be headache
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now