Date: prev next · Thread: first prev next last
2013 Archives by date, by thread · List index


On Wed, 02 Oct 2013 13:31:50 +0100
Brian Barker <b.m.barker@btinternet.com> wrote:

At 13:47 02/10/2013 +0200, Paul Steyn wrote:
In many programs this can be done with regular expressions. I just 
checked in LO Calc, and there is an option for regex.

We are talking about text (Writer) files here, but regular 
expressions work there too.
Yes, sorry, I should have specified; I just happened to have Calc open,
so assumed that if Calc could do it, so could Writer.

Of course, the "simple" way of doing this would be to search for 
"m2" and replace with "m(2)" (sorry, this is a text only email, so 
you'll have to imagine the second one has a superscript 2 instead of 
a 2 in brackets). When you know the term you are searching for, and 
know that you want to replace only part of it, you can just use a 
replacement term that is the same as the search term, with the 
correct part replaced.

The problem is that there appears to be no way in LibreOffice to set 
a replacement expression that is a mixture of formats - in this case 
Normal and Superscripted.  It's all or nothing.  Unless you know
better ...
I was actually referring to the previous comment about a way to replace
only part of the search term. But I see you are correct, the format
cannot be changed for only part of the search term, even using a regex.
Using undo, it appears that the replacement is done first, then the
formatting is done of the whole matched search term. I had assumed the
formatting would only be done on the replaced part.

For those interested, I will describe how to use a regex to replace
only part of a search term:

After checking the "Regular expressions" checkbox on the search window,
one can then use "(m)2" as the search term. The brackets make this a
numbered capturing group (one can even use names, but I won't go into
that syntax), and one can use that in the replacement term by using a
dollar sign to indicate a numbered group. So a replacement term of
"$12" would change nothing, as the "$1" refers to the first captured
group, the "m", and then the "2" is literal. The numbers confuse things
a little, so let's say instead we wanted to replace "man" with "woman",
we could do a search for "(man)" and use a replacement term of "wo$1".

For a more practical example that can't be done the simple way, let's
say you have written an article about John and Jane, but by accident
spelt their names without initial capitals throughout the document.
Instead of doing multiple search and replace operations, you can do it
all in one. If you use a search term of "j(ohn|ane)", this will match
all words that start with a "j" and are followed by either "ohn" or
"ane". Of course this matches both "john" and "jane", and the brackets
make the bit (whatever it is) that follows the "j" a numbered capture
group. Now in the replacement term you can use "J$1" to replace the
matched words with "J" and whatever initially followed the "j", i.e.
replace all "john"s with "John"s and all "jane"s with "Jane"s. This
example may seem a bit contrived, but there are plenty of times that
this does come in handy. Often, though, the regular expression can be
quite a bit more complex...

See http://www.regular-expressions.info/named.html for more info on
named capture groups, and that site is also an excellent reference for
regular expressions in general.

Regards

Paul

-- 
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.