extracting temperature degrees (celcius or fahrenheit) from string

前端 未结 1 1636
借酒劲吻你
借酒劲吻你 2021-01-20 13:57

I am using (char.*?char2) in order to extract subparts which starts with char1 and which ends with char2 from a string.

Now I

相关标签:
1条回答
  • 2021-01-20 14:41

    (\d+) ?°([CF])

    The first group should have the temperature, the second C or F.

    Expanding it to allow for a bit more variation:

    ([+-]?\d+(\.\d+)*)\s?°([CcFf])
    

    This would match any of these inputs, allowing for more than one space, or tab, lower case unit, decimal points and signs.

    Example python program:

    import re
    string = '''
    20°C
    2 °F
    It was cold, 2 °F in fact.
    30 °C
    -40 °C
    +2.3^I°c
    +2.3°c
    10°C
    '''
    pattern = r'([+-]?\d+(\.\d+)*)\s?°([CcFf])'
    print(re.findall(pattern, string))
    # Output:
    # [('20', '', 'C'), ('2', '', 'F'), ('2', '', 'F'), ('30', '', 'C'),
    # ('-40', '', 'C'), ('+2.3', '.3', 'c'), ('+2.3', '.3', 'c'),
    # ('10', '', 'C')]
    
    0 讨论(0)
提交回复
热议问题