How to test characters of a string

De ongekruisigde ongekruisigde at news.eternal-september.org
Wed Jun 8 04:07:40 EDT 2022


On 2022-06-08, dn <PythonList at DancesWithMice.info> wrote:
> On 08/06/2022 10.18, De ongekruisigde wrote:
>> On 2022-06-08, Christian Gollwitzer <auriocus at gmx.de> wrote:
>>> 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:
>> 
>> Regular expressions are overkill for this and much slower than the
>> simple isdigit based solution.
>
> ...
>
>> Regular expressions are indeeed extremely powerful and useful but I tend
>> to avoid them when there's a (faster) normal solution.
>
> Yes, simple solutions are (likely) easier to read.

Depending on the problem a regular expression may be the much simpler
solution. I love them for e.g. text parsing and use them all the time.
Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
like these:

  root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
  dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
  nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
  avahi:x:997:996:avahi-daemon privilege separation user:/var/empty:/run/current-system/sw/bin/nologin
  sshd:x:998:993:SSH privilege separation user:/var/empty:/run/current-system/sw/bin/nologin
  geoclue:x:999:998:Geoinformation service:/var/lib/geoclue:/run/current-system/sw/bin/nologin

Compare a regexp solution like this:

  >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
  >>> print(g.groups())
  ('geoclue', 'x', '999', '998', 'Geoinformation service', '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')

to the code one would require to process it manually, with all the edge
cases. The regexp surely reads much simpler (?).


> RegEx-s are more powerful (and well worth learning for this reason), but
> are only 'readable' to those who use them frequently.
>
> Has either of you performed a timeit comparison?

No need: the isdigit solution doesn't require the overhead of a regex
processor.

-- 
<StevenK> You're rewriting parts of Quake in *Python*?
<knghtbrd> MUAHAHAHA


More information about the Python-list mailing list