Amazing how some people bring out the heavy artillery, first! LOL!

If the question was how to remove any initial digits and perhaps whitespace in 
a string, it is fairly easy to do without any functions to test if there are 
digits before the title. I mean look at initial characters and move forward if 
it is between '0' and '9' or a space. Duh!

Sure, a regular expression that matches anything following a run of digits and 
whitespace and before a ".MPG" or the end of the entry will be easy to extract 
and compare after removing any left/right whitespace in both things being 
compared and coercing both to the same case.

But the solution may be doomed to failure when it sees things like:
"100 Letters" 
"1+1" 
"10,000 hours"
"1 Trillion Dollar$" 
"2,000 Light Years From Home"


So is it necessary to insist on an exact pattern of two digits followed by a 
space? 


That would fail on "44 Minutes", "40 Oz. Dream", "50 Mission Cap", "50 Ways to 
Say Goodbye", "99 Ways to Die" 

It looks to me like you need to compare TWICE just in case. If it matches in 
the original (perhaps with some normalization of case and whitespace, fine. If 
not will they match if one or both have something to remove as a prefix such as 
"02 ". And if you are comparing items where the same song is in two different 
numeric sequences on different disks, ...




-----Original Message-----
From: Christian Gollwitzer <aurio...@gmx.de>
To: python-list@python.org
Sent: Tue, Jun 7, 2022 6:01 pm
Subject: Re: How to test characters of a string

Am 07.06.22 um 21:56 schrieb Dave:
> It depends on the language I’m using, in Objective C, I’d use isNumeric, just 
> wanted to know what the equivalent is in Python.
> 

Your problem is also a typical case for regular expressions. You can 
create an expression for "starts with any number of digits plus optional 
whitespace" and then replace this with nothing:

> chris@linux-tb9f:~> ipython
> Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
> Type 'copyright', 'credits' or 'license' for more information
> IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
> 
> In [1]: import re                                                             
>                                                                               
>                              
> 
> In [2]: s='05 Trinket'                                                        
>                                                                               
>                             
> 
> In [3]: re.sub(r'^\d+\s*', '', s)                                             
>                                                                               
>                              
> Out[3]: 'Trinket'
> 

If it doesn't match, it will do nothing:

> In [4]: s='Es geht los'                                                       
>                                                                               
>                              
> 
> In [5]: re.sub(r'^\d+\s*', '', s)                                             
>                                                                               
>                              
> Out[5]: 'Es geht los'

Some people on this list don't like regexes but for tasks like this they 
are made and working well.

^ is "starts with"
\d is any digit
\s is any space
+ is at least one
* is nothing or one of

Christian




-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to