2016年5月23日 星期一

[python] regex 群組命名表示法

原本我只會

text = "123 is num"
re.match("^(\d+) is num$", text).group(1)

結果為 "123"

學了一下 Django 意外學到


text = "123 is num"
re.match("^(?P<num>\d+) is num$", text).group("num")
可以透過 ?P<variable_name>  為群組命名,程式碼可以更容易懂,還不錯。

沒有留言: