- 論壇徽章:
- 0
|
兩個(gè)問(wèn)題:
1.要匹配整行,是不是用match更合適。
match從字符串的開頭開始匹配,如果開頭位置沒有匹配成功,就算失敗了;而search會(huì)跳過(guò)開頭,繼續(xù)向后尋找是否有匹配的字符串。
2.正則表達(dá)式中:r'[^(]+([^,]*)\*([^,]*)' 中的[^,]是不是又包含了(
用這個(gè)試試:r'[^(]*\*+[^(]*'- import re
- _regexp_compile_cache = {}
- def Search(pattern, s):
- """Searches the string for the pattern, caching the compiled regexp."""
- if pattern not in _regexp_compile_cache:
- _regexp_compile_cache[pattern] = re.compile(pattern)
- return _regexp_compile_cache[pattern].match(s)
- readline = 'bool DimeterOmc::writeLog( int code_id, const char* log_desc, ...)'
- # typedef bool (*DYNCHECKFUNC)();'''
- match_point = Search(r'[^(]*\*+[^(]*', readline)
- print match_point.group(0)
復(fù)制代碼 |
|