[Tutor] How to find a word in a string - nearly correct

Phil phillor9 at gmail.com
Tue May 4 20:57:01 EDT 2021


On 5/5/21 8:02 am, Cameron Simpson wrote:
> On 04May2021 12:09, Phil <phillor9 at gmail.com> wrote:
>> On 4/5/21 11:20 am, Mats Wichmann wrote:
>>> On 5/3/21 7:09 PM, Phil wrote:
>> import re
>>
>> result = re.search(r'\b' + 'this' + '\W', test)
> You can write that in one go like this:
>
>      result = re.search(r'\bthis\W', test)
>
> OTOH, keeping them separate may aid debugging.
>
>> The output is 'this,' ,which is based on a white-space between words
>> rather than punctuation. The search continues.
> That's because you included the trailing "nonword" character in your
> regexp. Had you considered that "\b" matches a word boundary at the
> start _or_ the end?
>
>      \bthis\b
>
Thank you Cameron, more useful information to file away.

-- 

Regards,
Phil



More information about the Tutor mailing list