Python Regular Expression example

前端 未结 9 1540
天命终不由人
天命终不由人 2020-12-08 10:04

I want to write a simple regular expression in Python that extracts a number from HTML. The HTML sample is as follows:

Your number is 123
         


        
9条回答
  •  一个人的身影
    2020-12-08 10:14

    import re
    found = re.search("your number is (\d+)", "something.... Your number is 123 something...")
    
    if found:
        print found.group()[0]
    

    Here (\d+) is the grouping, since there is only one group [0] is used. When there are several groupings [grouping index] should be used.

提交回复
热议问题