Do regular expressions from the re module support word boundaries (b)?

Posted on

Solving problem is about exposing yourself to as many situations as possible like Do regular expressions from the re module support word boundaries (b)? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Do regular expressions from the re module support word boundaries (b)?, which can be followed any time. Take easy to follow this discuss.

Do regular expressions from the re module support word boundaries (b)?

While trying to learn a little more about regular expressions, a tutorial suggested that you can use the b to match a word boundary. However, the following snippet in the Python interpreter does not work as expected:

>>> x = 'one two three'
>>> y = re.search("btwob", x)

It should have been a match object if anything was matched, but it is None.

Is the b expression not supported in Python or am I using it wrong?

Asked By: D.C.

||

Answer #1:

Why don’t you try

word = 'two'
re.compile(r'b%sb' % word, re.I)

Output:

>>> word = 'two'
>>> k = re.compile(r'b%sb' % word, re.I)
>>> x = 'one two three'
>>> y = k.search( x)
>>> y
<_sre.SRE_Match object at 0x100418850>

Also forgot to mention, you should be using raw strings in your code

>>> x = 'one two three'
>>> y = re.search(r"btwob", x)
>>> y
<_sre.SRE_Match object at 0x100418a58>
>>> 
Answered By: pyfunc

Answer #2:

This will work: re.search(r"btwob", x)

When you write "b" in Python, it is a single character: "x08". Either escape the backslash like this:

"\b"

or write a raw string like this:

r"b"
Answered By: Bolo

Answer #3:

Just to explicitly explain why re.search("btwob", x) doesn’t work, it’s because b in a Python string is shorthand for a backspace character.

print("foobbar")
fobar

So the pattern "btwob" is looking for a backspace, followed by two, followed by another backspace, which the string you’re searching in (x = 'one two three') doesn’t have.

To allow re.search (or compile) to interpret the sequence b as a word boundary, either escape the backslashes ("\btwo\b") or use a raw string to create your pattern (r"btwob").

Answered By: Bill the Lizard

Answer #4:

Python documentation

https://docs.python.org/2/library/re.html#regular-expression-syntax

b

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that formally, b is defined as the boundary between a w and a W character (or vice versa), or between w and the beginning/end of the string, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. For example, r’bfoob’ matches ‘foo’, ‘foo.’, ‘(foo)’, ‘bar foo baz’ but not ‘foobar’ or ‘foo3’. Inside a character range, b represents the backspace character, for compatibility with Python’s string literals.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *