if statement - How to apply conditional treatment with line.endswith(x) where x is a regex result? -
i trying apply conditional treatment lines in file (symbolised list values in list demonstration purposes below) , use regex function in endswith(x) method x range page-[1-100]).
import re lines = ['http://test.com','http://test.com/page-1','http://test.com/page-2'] line in lines: if line.startswith('http') , line.endswith('page-2'): print line so required functionality if value starts http , ends page in range of 1-100 returned.
edit: after reflecting on this, guess corollary questions are:
- how make regex pattern ie
page-[1-100]variable? - how use variable eg
xinendswith(x)
edit:
this not answer original question (ie not use startswith() , endswith()), , have no idea if there problems this, solution used (because achieved same functionality):
import re lines = ['http://test.com','http://test.com/page-1','http://test.com/page-100'] line in lines: match_beg = re.search( r'^http://', line) match_both = re.search( r'^http://.*page-(?:[1-9]|[1-9]\d|100)$', line) if match_beg , not match_both: print match_beg.group() elif match_beg , match_both: print match_both.group()
i don't know python enough paste usable code, far regular expression concerned, rather trivial do:
page-(?:[2-9]|[1-9]\d|100)$ what expression match:
page-fixed string matched 1:1 (case insensitive if set options that).(?:...)non-capturing group that's used separating following branching.|act "either or" expressions being left/right.[2-9]match numerical range, i.e. 2-9.[1-9]\dmatch 2 digit number (10-99);\dmatches digit.100again plain , simple match.$match line end or end of string (again based on settings).
using expression don't use specific "ends with" functionality (that's given through using $).
considering have parse whole string anyway, may include "begins with" check well, shouldn't cause additional overhead (at least none you'd notice):
^http://.*page-(?:[2-9]|[1-9]\d|100)$ ^matches beginning of line or string (based on settings).http://once again plain match..match character.*quantifier "none or more" previous expression.
Comments
Post a Comment