I\'m trying to get the elements in an HTML doc that contain the following pattern of text: #\\S{11}
this is cool #12345678901 <
this is cool #12345678901
With bs4 (Beautiful Soup 4), the OP's attempt works exactly like expected:
from bs4 import BeautifulSoup soup = BeautifulSoup(" this is cool #12345678901 ") soup('h2',text=re.compile(r' #\S{11}'))
returns [ this is cool #12345678901 ].
[ this is cool #12345678901 ]