[Zope] Using RegEx in Script(Python)

Andreas Pakulat ap125@informatik.uni-rostock.de
Fri, 27 Jun 2003 02:56:13 +0200


On 27.Jun 2003 - 02:48:24, Andreas Pakulat wrote:
> On 27.Jun 2003 - 01:53:27, Steffen Hausmann wrote:
> > On Fri, Jun 27, 2003 at 01:09:27AM +0200, Andreas Pakulat wrote:
> > > I wanted to know if the above can be done? What I need is a function
> > > that replaces every character of a string, that is not in [a-zA-Z1-9]
> > > with an underscore. I want to use this to automatically create an
> > > Object-Id from a title, to create a new Object.
> > > 
> > > If this is not possible directly within a Script(Python), can it be done
> > > using an ExternalMethod? I suppose yes.
> > 
> > In order to use regular expressions in python you need to 'import re'.
> > 
> > After that you can use 'sub(pattern, repl, string[, count])' to
> > substitue a string.
> > 
> > For more information on regexps in python take a look at
> > http://www.python.org/doc/current/lib/node99.html#l2h-732
> 
> The problem is that import re leads to an BasiAuth Dialog, which cannot
> be fulfilled. I mean, I'm already logged in to the ZMI but do get a
> BasicAuth Dialog if I execute the script that tries to import re. I
> think that regexp's are not completely included in Zope - I looked at
> some parts of the python-code and did not find the sub-function at the
> point where all the other re-functions are mad "safe". 
> 
> I have a workaround that iterates over the string as list and changes
> everything that is not alphanumeric into '_'. As the strings I replace
> won't get very long (max 100 chars) this should be fast enough. 
> 
> I also tried the external method, but I think I did something wrong, the
> following returns the unchanged string:
> 
> re.sub('[^a-zA-Z0-9]','WS/04','_')

It works right, but I made a mistake, having the replacement and the
original string interchanged :(

Andreas

-- 
Try to get all of your posthumous medals in advance.