[Tutor] a little help with the re odule

nathan tech nathan-tech at hotmail.com
Sat Jul 18 08:51:22 EDT 2020


As always, the exact answer I was looking for, thanks Alan!

I'll definitely check out the tutorial you mentioned as well.


On 18/07/2020 12:43, Alan Gauld via Tutor wrote:
> On 18/07/2020 11:57, nathan tech wrote:
>
>> I'm trying to do a little bit of work with the re module but have rather
>> gotten stuck and am hoping someone will be able to fill in the gaps in
>> my understanding.
> Sure, but first the usual caveat. regex are immensely powerful and quite
> addictive. But very often the regular string methods are faster and
> simpler. Don't let the cleverness of regex seduce you into over use.
>
> That having been said...
>
>> The match pattern is: The * sat on the *
>>
>> Now with this example a bit of work with the \w match would work fine.
> For single words yes. But you can combine regex with multipliers and
> optional controls etc. regex is like a programming language with many
> of the same control structures.
>
> Also there will always be several ways to do it. It is worth bookmarking
> one of the online regex testers to try out your regex to ensure it does
> what you expect.
>
>> What I can not figure out is lets say I have the match pattern: The *
>> sat on the *
>>
>> What I want to do is for that to match all of the following:
>>
>> the cat sat on the mat
>> The dog sat on the shoe
> While \w will work for a single word it gets more complex
> with multiple words.
>
>> The dog and the cat sat on the hoverboard.
>> The big angry mouse sat on the mat and ate it.
> This gets more complex because you want to match multiple words between
> The and sat. The simplest way here is probably the .* combination
> (zero or more repetitions of any character).
>
> The .* sat on the .*
>
> Or + if you want at least 1 character.
>
> The .+ sat on the .+
>
> But there are a myriad other ways to grab that content depending
> on what exactly you plan on doing with the matches once you grab them.
>
> You might find my tutorial topic on regex useful.
>


More information about the Tutor mailing list