???
?, ???? ???? ?? ??? ?? ???. ? ????? ???? ????, ???? ???? ??? ?? ??? ?????. ?? ?? ?? ?? ??? ?????? ??? ???? ???? ????? ???? ?????.
???? ???: {min,max}???. min? max? ?? ?? ?? ?????. ??? ?? max? ??? ?? max?? ??? ????. ??? ???? ?? ???? ???? ?????. ?? ??, b[1-9][0-9]{3}b? 1000?? 9999 ??? ??? ????("b"? ?? ??? ???) b[1-9][0-9]{ 2,4} b, 100 ~ 99999 ??? ??? ?????.
?? ????? 4~7?? ????? ?? ?? ???????.
import re a = 'java*&39android##@@python' # 數(shù)量詞 findall = re.findall('[a-z]{4,7}', a) print(findall)
?? ??:
['java', 'android', 'python']
???? Greedy? Non-greedy? ??? ?? ?????. ?? ?? ??? ?? ???????:
Greedy ??: ?? ???? ? ?? ?? ?? ?????. ???? ??? ?? ??? ??? ?? ?? ???? ????? ???? ?? ??? ??????. ??? 0? ? ???. ? ??? ??? ? ?? ??? ?? ???? ? ?? ?? ??? ??? ?? ?????.
Lazy ??: ???? ???? ???? ???? ??? ?? ?? ??? ???? ?? ?????. ???? ? ??? ? ?? ?? ??? ?????. ???? ???? ?? ?? ??? ??? ?? ?????.
?? ?? ??? ?? ??, ? ??? ??? ????? ??? ?? ????
???? ??? ?? ?? ?? ???? ?? ?? ??? ?? ?????.
import re a = 'java*&39android##@@python' # 貪婪與非貪婪 re_findall = re.findall('[a-z]{4,7}?', a) print(re_findall)
?? ??? ??? ????.
['java', 'andr', 'pyth']
?? ???? ? ? ??? android??? andr? ????? ?????. , Python? pyth? ????? ?????. ???? Lazy ??? ???? ?????.
?? ??? ??? ? ?? ?? ??? ????. ?:
?:告訴引擎匹配前導(dǎo)字符 0 次或 1 次 +:告訴引擎匹配前導(dǎo)字符 1 次或多次 *:告訴引擎匹配前導(dǎo)字符 0 次或多次
? ??? ?? ???? ???? ?? ????.
?? | ??? | ?? |
? | ? ? | 0? ?? 1?? ??, ?? {0,1} |
+ | +? | ? ? ? ?? ?????. ?? {1,} |
* | *? ?????. | ? 0? ?? ?????. ?? {0,} |
{n} | {n}? ?????. | ??? n? ????? |
{n,m} | {n,m}? | ??? n?? ??? m? ?? ????? |
{n,} | {n,}? | n? ?? |