Regular expressions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Nov 3 22:35:23 EST 2015


On Wednesday 04 November 2015 03:20, Chris Angelico wrote:

> On Wed, Nov 4, 2015 at 3:10 AM, Seymore4Head
> <Seymore4Head at hotmail.invalid> wrote:
>> Yes I knew that -1 represents the end character.  It is not a question
>> of trying to accomplish anything.  I was just practicing with regex
>> and wasn't sure how to express a * since it was one of the
>> instructions.
> 
> In that case, it's nothing to do with ending a string. 

Seymore never said anything about ending a string.


> What you really
> want to know is: How do you match a '*' using a regular expression?

He may want to know that too, but that's not what he asked for. He asked how 
to match an asterisk at the end of the line.


> Which is what MRAB answered, courtesy of a working crystal ball: You
> use '\*'. Everything about the end of the string is irrelevant.

Not at all -- matching "\*" will find lines *beginning* with an asterisk if 
you use re.match, and lines containing an asterisk *anywhere* in the line if 
you use re.search.

I say "line" because the most common use for re.match and re.search is to 
match against a single line of text, but of course regexes can operate on 
multiline blocks of text, with or without multiline mode turned on.

And calling it "a working crystal ball" is somewhat of an exaggeration. The 
plain English meaning of Seymore's plain English question is easily 
understood: he wants to know how to match an asterisk at the end of the 
line, just like he said :-P


> (So,
> too, are all the comments about using [-1] or string methods. But we
> weren't to know that.)

If MRAB could understand what he wanted, I'm sure most others could have 
too.



-- 
Steve




More information about the Python-list mailing list