4

I need to find and replace tags in an HTML string. What I need is to find few specific custom tags (tagA, tagB, tagC), and replace them with other strings, different for each tag.

What will be more efficient?

  • To run a Regex find and replace for each tag
  • run one regex search to find all tags, iterate each result and look for its type be the tag name
  • other way that I didn't think of

Thanks

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • 1
    In my opinion, an xpath query first (there are several gorgeous string functions as well) and then replace these tags based on your regex. – Jan Feb 24 '16 at 07:58

1 Answers1

0

The most efficient would be to run a single regex and replace taking into on match value. Don't know about Swift but in Python you can pass a handler to the replace function https://docs.python.org/2/library/re.html#re.sub

A regex can be something like https://regex101.com/r/jE1pE0/1

Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52