regex - how to print previous string after selecting specific string python -


i want find word appear before keyword (specified , searched me) , print out result. tried below code gives me after words not before...

    str = "phone has better display, speaker. phone has average display"     p1 = re.search(r"(display>=?)(.*)", str)     if p1 none:        return none     return p1.groups() 

this code gives me

    , speaker. phone has average display 

but want

    better,average 

you can use positive lookahead, findall instead of search:

>>> p = re.compile(r'(\w+)\s+(?=display)') >>> p.findall(str) ['better', 'average'] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -