2

Given this string:

SET @text = "<a href=""https://salesforce.stackexchange.com""target=""_blank"">Salesforce</a>
<a href=""https://stackoverflow.com"" target=""_blank"">Everything else</a>"

And this function:

SET @matching = '\s+(?:[^>]*?\s+)?href="([^"]*)"'
SET @output = RegExMatch(@text, @matching, 1)

How can I manage to find and print an arbitrary number of matches in my string? E.g. in above example, I would like to present e.g. a rowset with each URL in each row.

4
  • First question: Why are the hrefs with two ", this is kinda wrong (or is this the challenge). Second: Is each line seperated by linefeeds or is it a string that is connected without any seperator (or only maybe </a>? Oct 10, 2019 at 8:31
  • Just to give you a solution idea that came to my mind: count </a>s and iterate through the number of with multiple regexmatches with looping (variable i). RegExMatch(@text, @matching, @i) Oct 10, 2019 at 8:36
  • 1
    The double-double-quotes, are for escaping purposes, as per developer.salesforce.com/docs/… Oct 10, 2019 at 8:40
  • Personally i wouldnt write it that way you did. I would use: Set @text = '<a href="https://salesforce.stackexchange.com" target="_blank"> ... ' i would nearly think that your code is wrong ;) Oct 10, 2019 at 9:20

1 Answer 1

5

Not a pretty solution, but here's what I use:

%%[
SET @text = "<a href=""https://salesforce.stackexchange.com""target=""_blank"">Salesforce</a><a href=""https://stackoverflow.com"" target=""_blank"">Everything else</a><a href=""https://google.com"" target=""_blank"">Search</a>"
SET @href = BuildRowsetFromString(@text, 'href="')
for @i = 1 to Rowcount(@href) do
SET @endhref = BuildRowsetFromString(Field(Row(@href, @i),1), '"')
OutputLine(Concat(Field(Row(@endhref, 1),1),"<br>"))
next @i
]%% 

Output:
https://salesforce.stackexchange.com
https://stackoverflow.com
https://google.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.