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

@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?

tdf#70142

And this would not be possible at any case, even after the issue above is resolved. Lists are metadata of paragraphs, and you can’t just change automatically computed things.

You could possibly use a macro to convert lists to text, before the replacement. See How to convert numbered/bulleted lists to plain text in Writer? - #4 by mikekaganski.

This is possible only if your items are formatted with styles, here a combination of a paragraph style and list style. Formally, only the list style is necessary.

You must first understand what is the identity (or family) of a list, because you may have many lists in your document. Lists belong in the same “family” when their numbering/bullet is defined by the same list style. This excludes Format>Bullet & Numbering or equivalently toolbar buttons which are in fact direct formatting.

You can change in a single step the numbering/bullet of a family by customising the list style. The change will be instantly applied to all lists of the family, hence the importance of attributing a different list style to lists which have different semantic values.

  • open the list style configuration dialog just like you would do for any other style
  • go to Customize tab
  • choose from Number drop-down menu the kind of numbering you want for this level
  • in case of Bullet, you can assign any character as bullet with button Select aside Character

This can be done separately on all levels of the list.

Caveat: don’t use Unordered, Ordered, Outline, Image tabs once you have begun customising. They preset value in Position and Customize which erases your own customisation.

@mikekaganski, solution you have provided converts the ‘fields’ to plain text. Then I can replace new plain text with regular expression in Find field: ^\d+\. with & in Replace field.

I am posting solution here, because it was little bit unclear first for me what to do.

But when above macro is executed the side effect: it adds horizontal tab at my existing document Heading 2 styles. But I can remove that with:
a) Check Paragraph Style. Find field: Heading 2 and click on Find All button.
b) Uncheck Paragraph Style. Check Current selection only and check Regular expressions. Find field: \t and Replace field empty and Replace All.
@ajlittoz, I am not using List Styles, but Ordered List which is direct formatting.

Now I have one more problem… For each of transformation I have recorded macro that is running without an issue. Excellent. But now I would like to write a macro that calls other macros, so I need to run only one single macro manually and all others are run but this master macro. I have named this master macro RUN_ALL_MACROS and it is pretty simple to run other macros just execute “call <macro_name>”, but how do I run ConvertOrderedListFieldsToPainText that is not in My Macros section?

When I run above RUN_ALL_MACROS macro I get error: