How to find/replace/tag paragraph styles (regular expressions)?

In LibreOffice Writer v7.4.0.3 I would like to find:

some text

and replace it with (append two == and suffix two ==).

==some text==

I tried the following:

  1. New Writer document.
  2. In document write: some text
  3. Select text and select from menu Format | Character
  4. In Font tab in Western sub-tab select Font= Arial and click on OK** button.
  5. Menu Edit | Find and Replace
  6. Click on Find data field and type in: (.*)
  7. Click on Format button.
  8. Select the same as in step 4.
  9. Click on Replace** data field and type in: ==$1==
  10. Check the Regular expressions check box and click on Replace All button.
    Result: Text “some text” gets correctly replaced with “==some text==”.

Now I would like to do the same with “Heading 2” style.

  1. New Writer document.
  2. In document write: some text
  3. Select text and right click and select Paragraph | Heading 2 (or select Heading 2 style from toolbar).
  4. Right click on text and select Paragraph | Edit Style.
  5. Repeat steps from 4 to 10 from previous section instructions above.
    Result: text does not get replaced. In dialog is also displayed “Search key not found”.

How to find/replace text when text is using styles instead of direct editing.

P.S. I closed down LibreOffice and open it, so all of the find/replace settings are lost from about steps and in Find and Replace dialog I have tried to check Paragraph Styles, but in this case Regular expressions are greyed out and it looks like you can only replace one paragraph style with another paragraph style, so not really useful for find/replace in my case.

Not sure I understand correctly your second trial.


Using (.*) is “dangerous” because it matches a whole paragraph, i.e. it is not selective at all. You end up with your paragraph prefixedand suffixed with ==. This may not be what you expect if you intend to “highlight” only a word.


Scanning text works without taking formatting into account (unless you explicitly request it). So, your second test should work like the first one. Even if you press Replace with cursor after the last Find occurrence, this will select (but not replace) the very first occurrence.

Paragraph Styles check box is used to search and replace styles not text.

IMHO you are searching some specific text in a/some paragraph/s (formatted with character styles) and you want to apply another character style.
Be aware that the regular SEARCH&REPLACE tool cannot do that. For that purpose use AltSearch.oxt.

In my screenshot AltSearch.oxt searches every character (resp. text in general) which is formatted with Quotation style. The words are selected.
To apply another character style…

  • leave AltSearch.oxt

  • characters (words) are still selected

  • apply another character style

For finding text parts you could use .* but be advised that this search term is greedy (see @ajlittoz’ note!).
Upload some sample text to better check what you intend to do.

@pavion If I have misunderstood you so please specify your question and add more details.





EDIT
Your topic refers to page styles. Is this what is really meant? If no so please correct the topic.

1 Like

@ajlittoz, I need to search for formatted text. Why? Because in LibreOffice I have several paragraph styles and only paragraph style “Heading 2” should be added “==” characters at front and after.

But what I am trying to explain in my question is, if direct editing is used to format text then find/replace works fine. But if the same formatting (the same font) is used to find/replace used in paragraph styles then replace does now replaces the text.

I am aware (.*) is used to replace paragraphs, but in my case I only have text in one line (because they are headings).

By the way, you have written that second case should be also working. Have you tested the case and is it working in your environment?

@Grantler, I have tested the Altsearch extension, but can’t figure it out how does this extension solves the problem in my case. Can you please write step by step instructions.

Yes, this why I don’t understand why it fails at your computer. While searching for text, you have absolutely no need to customise the criteria to include formatting. On the contrary, verify that everything is clear (unset).

I have probably too much simplified my problem. Bellow is example. “First heading” text is in paragraph style “Heading 2”, then “Lorem ipsum” is paragraph style “Text body” and then again “Second heading” is paragraph style “Heading 2” again and “Lorem ipsum” is paragraph style “Text body”.


First heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.

Second heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.

Third heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.


I need to tell LibreOffice that only “Heading 2” paragraph style must be replaced with “==” before and after the text to get the result:


== First heading ==

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.

== Second heading ==

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.

== Third heading ==

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla enim augue, lacinia vel lorem nec, aliquet egestas urna. Suspendisse vitae.


I’m afraid this is not possible with the standard Find & Replace.

However, if you accept a slight change in your aspect, there is a very simple solution. The suggested change is to prefix only your Heading 2 with “==” (no suffix; your heading is only “== First heading”.

You customise Tools>Chapter Numbering, Numbering tab:

  • select Level 2
  • in Separator Before, enter "== " (without quotes; they are here only to emphasise the space after the equal signs)
  • OK

Here’s a workaround:

  • First, modify the Heading 2 paragraph style and set the font size to a unique value. Save your document with a new name.
  • Open the Find and replace dialog box. Enable Regular Expressions. In the Search box, enter (.*). Click Format and set the Font size to the new font size in the Heading 2 style. OK to close. Now you can enable Including Styles.
  • In the Replace box, enter == $1 ==. Click Find Next to verify that it properly selects what you want, then Replace all.
  • Change the font size for the heading 2 style back to the original.
1 Like

You better had given your example earlier instead of complicated explanations…
My solution to your example is…


HTH

3 Likes

@anon87010807, great answer, your suggestion works fine.

To recap into my original question.
a) For second section of my instructions me make sure that font (or combination of font+font size+intalic+bold) in step 5 is unique for whole document to prevent replacing too much text (some other text beside headings).
b) Between step 9 and step 10 there should be 9a step: check the Including Styles checkbox.

@Grantler, this is even better solution. Excellent.

If I understand correctly at first picture you have selected options in red rectangles and then pressed Find All button.

In second picture you have checked Current selection only button and unchecking Paragraph Styles which made Regular Expressions check box possible to check and I have checked it. Then typed in regular expressions and Replace All button.

Additional questions related to your reply:

  1. In Find field what exactly does ^.*?$ means?
    ^ is probably at the start of paragraph
    $ is probably at the end of paragraph.
    .* is probably any character
    But what is “?” for?
  2. How is “your” regular expression better then “mine”?
  3. What exactly does & expressions means in Replace field and how is it better then “mine” $1 expression?
1 Like

https://help.libreoffice.org/latest/hr/text/shared/01/02100001.html?&DbPAR=SHARED&System=WIN

1 Like

ADDITIONAL INFO HOW TO AUTOMATE THE PROCESS (if some other users are reading this and having the same problem).

I can now record a macro and do the find/replace automatically for all sort of find/replace.

To record macro:

  1. Tools | Options
  2. LibreOffice | Advance and check Enable macro recording and OK button.
  3. Tools | Macro | Record Macro
  4. Do all steps to find/replace text.
  5. Click on Stop Recording button.
  6. Dialog opens. In Macro Name field type in some name and in Save Macro In field click at My Macros | Standard to expand selection and then click on Module1 and Save button.
    7.Now to test press Ctrl+Z to revert find/replace.
  7. Now run macro with Tools | Macro | Run macro and select macro from Library and Macro Name fields and click on Run button.
    That’s it. Using macro whole document can be replaced with few clicks.

@mariosv, thank you for the link to regular expressions help. Now I see “?” is for zero or one regular expression term. I did some tests and I see no difference in this case using “?” or without “?”.
Regular expression $1 means the first group inside brackets. Because in my case I have only one pair of brackets this behavior is the same as &

Also I tested little bit more and the simplest (the least characters to type) regular expression for my case looks to be:

  • Find field: .*
  • Replace field: ==&==

The “?” is superfluous, you are right.
I had not tested all the possible search terms. In my reasonings I had recalled my first answer as I thought you have to find parts of paragraph and not the entire paragraph. So my search term was a bit too redundant. Nevertheless it worked. :wink:

1 Like

As explained in the detailed ICU regular expression syntax documentation (linked from the help page), *? is a syntax for “Match 0 or more times. Match as few times as possible”, unlike simple * meaning “Match as many times as possible”.

Indeed, it has no meaning when you use ^...$, because Writer’s Find&Replace only handles a single paragraph at a time, never works across several paragraphs.

2 Likes

Thanks for clarification!

Indeed you can’t, because quoting without context, freely stripping random parts, is prone to errors.

“Never” means “No, it does not work across paragraphs”.

1 Like

I have one more little problem that is related to regular expression problem. Type in text “first option” in first line and “second option” in second line. Select whole text and press F12 or from toolbar select Toggle Ordered List I get:


  1. first option
  2. second option

I would like to replace numbers with “#” character.
I tried using regular expression in Find: \d+\. field and in Replace field: # But when I press Replace All button nothing is found.

Looks like it is something special for this search, because Ordered List are some kind of fields and not regular text. How to search through fields and execute replace?