[Wylug-help] OpenOffice Regular Expression Search and Replace

Smylers Smylers at stripey.com
Thu May 31 09:42:42 BST 2007


Dave Fisher writes:

> does anyone here know if OOo's search and replace command actually
> implements all of the regular expressions listed in the help page?

I didn't even know that OO implemented regexes till reading your mail.

> OOo does seem to be able to find a class like [A-Z]

Yup.

> ... but if I try to replace that with [a-z] all it does is replace
> every uppercase letter with the 5-character string literal '[a-z]'.

That's exactly what I'd expect.  It's also what s/[A-Z]/[a-z]/ would do
in Perl, Vim, and so on.  Regular expressions are patterns to match;
they make no sense (in general) as the replacement string where you
aren't trying to match anything; for example things like this are
obviously meaningless:

  s/".*"/[A-Z][0-9]+/

What you're wanting to do, where you specify a range for both matching
and replacement, only makes sense under particular conditions:

* You're only trying to match a single character.
* The replacement range has the same number of characters as the range
  being matched.

While that happens to be true in your regex, because it isn't generally
true for all regexes, regexes simply don't try to implement this sort of
thing.  Instead what does provide this, and enforces the above
conditions[*0], is transliteration, for example tr/A-Z/a-z/ in Perl or
the tr command.

I'm not aware of OO supporting transliteration (but I haven't looked,
and see my above lack of knowledge of it doing regexes).

> I can't seem to write any incantation which will make OOo use POSIX
> class expressions (e.g. [:space:], [:lower:], etc) to find

Ditto.  Posix classes are only meaningful inside character classes, so
you have to write something like [[:digit:]] or [[:digit:]a-z] to use
them.  The OO documentation doesn't mention this (nor make any claims to
being Posix compatible), but trying with both single and double square
brackets I didn't induce a match.  This seems to be a bug.

> So, for example, how do people downcase a selection?

It may not be possible with the Find & Replace feature.  Vim, Perl,
mod_rewrite and other substitution features which support this make
available special symbols (not regexes) in the replacement field:

* Commands such as \l and \L for affecting the case of the following
  text.

* Backreferences such as \1 or $1 for referring to portions of the
  original matched text.

I can't see any evidence of OO providing these things.

But searching the built-in help index for "lowercase" suggests that the
menu item Format > Change Case > Lowercase should do what you want.

  [*0]  Actually transliteration implementations often have options for
  specifying what to do if the lists are of different lengths.  But
  that's irrelevant here.

Smylers



More information about the Wylug-help mailing list