So I have a string something like this, which is html content with dynamic Tokens embedded in them. Now I need to capture some token information in a map[string]string, which is key of token name to the UUID.
I can check the string to see if a token that I care about example emailAppointmentCancellation
is present in this block by using strings.contains method. Now if it contains this token, I need to grab the UUID associated with it, which is till the end of the quotes mark. How can I go about it. Would I need some regex expression for that
<div style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);"><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);">Hello This is a friendly reminder about your upcoming appointment with </p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);"><br></p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);">Your appointment is: <span style="color: rgb(0, 0, 0);">{{Token "Stamps:appointmentDate" .}}</span>, <span style="color: rgb(0, 0, 0);">{{Token "Stamps:appointmentTime" .}}</span></p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);"><br></p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);">We appreciate your time and look forward to seeing you then!</p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);"><br></p><p style="font-family: Arial; font-size: 16px; line-height: 1.4; margin: 0px; color: rgb(0, 0, 0);">Sincerely, <span style="color: rgb(0, 0, 0);">{{Token "Forms:emailAppointmentCancellation:0386DA88-B658-43E8-863C-296A22D732FF" .}}</span><br><br><br></div>
CodePudding user response:
Regex
Yeah I think a regex would be an easy solution. I like to use https://regex101.com/ for trying it out.
A simple example: https://regex101.com/r/ioRC6e/1
You can use the regex package from the standard library to compile this.
String Manipulation
If you can be very certain about the data format, you could also just go through the string and find your keys:
https://goplay.tools/snippet/eRu-sKJr-se
CodePudding user response:
Thank you all. This is the regex i used finally
regx := regexp.MustCompile("Forms:" v ":([-0-9a-zA-Z] )")
r := string(regx.Find(data))
where v = emailAppointmentCancellation
or any other token that I care about
and data is the string in which i am finding the token.