Python string search - starting from a specific position within a string
Question: I have the following text stored in a variable which has additional text
before and after it:

`'content="80.96"abcd'`

I have a search variable from which I find out the location of: content="

But then I need another search to find the location of the second quotation
mark, using the end position which I found out from the previous search as the
starting point for this second search. Is it possible to do that kind of
search, from a certain starting position in a string, using re.search?

You see, my goal is to get the 80.96, the price of some stock. However, for
another stock, it could be fewer or greater number of digits. My ultimate goal
is to search for the position of the second " so that I can calculate the
length of the stock price and extract it.

Using Python 3.4.3

Thanks


Answer: If I understand you question correctly. You just want to get the decimal from
the string. Assuming that all strings are of the same format i.e.
`content="<decimal>"cccc`.

You can split the string and then use a regex to do that:

    
    
    In [1]: a = 'content="80.96"abcd'
    
    In [2]: a.split('=')
    Out[2]: ['content', '"80.96"abcd']
    
    In [3]: b = a.split('=')[1]
    
    In [4]: b
    Out[4]: '"80.96"abcd'
    
    In [5]: import re
    
    In [6]: p = re.compile(ur'\"(\d*\.?\d*)\"')
    
    In [7]: re.search(p, b)
    Out[7]: <_sre.SRE_Match at 0x3ad2af8>
    
    In [8]: obj = re.search(p, b)
    
    In [9]: obj.group()
    Out[9]: '"80.96"'
    
    In [10]: s = obj.group()
    
    In [11]: s
    Out[11]: '"80.96"'
    
    In [12]: s[1 : -1]
    Out[12]: '80.96'
    

As a program it would be:

    
    
    import re
    
    def getDecimal(string):
        part_2 = string.split('=')[1]
        pattern = re.compile(ur'\"(\d*\.?\d*)\"')
        obj = re.search(pattern, part_2)
        return obj.group()[1 : -1]
    
    print(getDecimal('content="83520.96652"asdf')) # Output: 83520.96652
    

