Saturday, 15 August 2015

python - Can't get a regex to handle brackets properly -



python - Can't get a regex to handle brackets properly -

apologies vague title.

i'm trying regex searches , oks this:

"brand new song [demonstration]"

by finding "[demonstration]" somewhere in string, using re.search(). here's illustration of think should homecoming true:

bool (re.search (r"\b\[demonstration\]\b", "brand new song [demonstration]", re.ignorecase))

going simpler, returns false:

bool (re.search (r"\b\[\b", " [ "))

i've been using \b origin , ends of captured string because it's supposed represent empty space string @ origin or end of word (as per documentation here), , don't see i'm messing up.

continuing befuddlement, next returns true:

bool (re.search (r"\b\[\b", "_[_"))

which as confusing because \b defined partly "...whitespace or non-alphanumeric, non-underscore character." so, please help me find stupid detail i'm missing, thanks!

you need remove word boundaries \b pattern.

>>> import re >>> s = 'brand new song [demonstration]' >>> bool(re.search (r'\[demonstration\]', s, re.ignorecase)) true

a word boundary not consume characters, asserts on 1 side there word character, , on other side there not. stated in regular-expressions.info documentation:

there 3 different positions qualify word boundaries:

before first character in string, if first character word character. after lastly character in string, if lastly character word character. between 2 characters in string, 1 word character , other not word character.

python regex

No comments:

Post a Comment