Python: Finding text, transforming it, replacing it
Question: Can't seem to articulate this problem well enough to look this up. I'm looking
to use regular expressions to identify certain strings of text, transform
them, and then replace that string with the transformed text.

For instance: "Some random stock symbol: MSFT AAPL INTC"

If I wanted to replace the stock symbol with say a stock symbol that is linked
with HTML, what's the best way to go about doing this?

I can write a regular expression to find the stock symbol and do a simple
substitution of a fixed string, but I don't know how to tell Python to "find
string in text, transform it, and replace the string with the transformed
text"

In this particular example, I could split the string on a space and iterate
through the list, but I'd like something that just finds and replaces the text
as Python finds it as there might be cases where a consistent delimiter might
not be available.


Answer: 
        import re
    
        string = 'Some random stock symbol: MSFT AAPL INTC'
        tickers = re.findall('[A-Z]{4}', string)
    
        for x in tickers:
           string = re.sub(x, 'example.com/'+x, string)
    

This returns 'Some random stock symbol: example.com/MSFT example.com/AAPL
example.com/INTC'

