[Tutor] Re: Re: Testing for punctuation in a string

Greg Brunet gbrunet at sempersoft.com
Wed Oct 15 15:22:09 EDT 2003


Hi Michael:

"Michael Janssen" <Janssen at rz.uni-frankfurt.de> wrote in message
news:Pine.A41.4.56.0310131536310.1526002 at hermes-22.rz.uni-frankfurt.de...

> When I may take a look at your latest approach (Testing legality by
> definig a pattern the Field Name must match):
>
> > p=re.compile('^[a-zA-Z]\w*$')
> > def fldNameValid(fldName):
> >     return p.match(fldName) != None
>
> Doing things the other way around as in ones own goal-definition
> (supposed I can take the thread's subject as such a thing ;-) might
> be a clever choice in case it becomes clear that it's better than the
> "former way around". Nevertheless it might introduce some unforeseen
> results: Now, your pattern rejects whitespace and commas; Field Names
> starting with numbers or an underscore will be rejected. All this
might
> be perfectly what you needs but it doesn't do the "Testing for
> punctuation in a string" job any longer.
>
> Beside this (and given that the additional restrictions for valid
Field
> Names are what you need), you're solution will do the job, and you
won't
> feel much of a performance impact as long as you don't want to parse a
> serveral kB's commandline ;-)

Thanks for the comments.  You are right that my original request (just
testing for any punctuation in a string) was a simplification of my
complete needs (to make sure that I had valid field names).  Whereas I
originally allowed commas (as field separators), once I start checking
individual fields they are obviously not valid.  After my original
posting, I decided that I really should be checking for all invalid
field names (e.g. starting with a numeral), thus the need to re-work /
improve my objective.  The answer to my original question is still of
interest & value to me, since I am not completely up to speed on the
'correct' or most Pythonic way of doing various string operations.  So
thanks to all who helped out on this,

-- Greg





More information about the Tutor mailing list