Find by formatting question

Is it possible to search through the project or a document for italic text and add some characters around it or in front of it automatically?

In Edit > Find > Find by Formatting there isn’t this option, and for some reason I even fail in finding any italic text although there certainly are some within the project. I have selected Find: Character Format and chosen italic in the bar. See screenshot:

app.box.com/s/2jv0dzmdz4s7qxxivk78gcinie1tof6h

I can’t fill “Containg text” because I need to find any italic text. Perhaps a regular expression could work here?

It would be best if the character adding happened while compiling though. That’s when I really need it.

If what you need in the end is something that works during compile, then we may have something that works out well for you in the long-term, but for now, that should also mean you could use a word processor with these types of tools already available, since it’s the final output that matters? We don’t have any plans to add complicated format-based search and replace in the editor, to be clear—in large part because comprehensive tools for doing this already exist, and we spend more development time on writing tools than formatting tools since the former is the focus and purpose of the software.

Meanwhile, as to your settings, are you looking for italicised text that is also struck-through and marked to be attached to the following text? :slight_smile: You should disable those search rules if not. You can leave the text field blank, you only need to use that if you only wish to find italic text with that specific string.

:blush: right, those checkmarks shouldn’t be there. I did test without them but I suppose some other settings were wrong at the time. I dimly thought that the found text would get Strikethrough to highlight it. Does that make any sense? To my tired mind I guess it did. And the other, Keep with next: I still have no idea what it means.

Anyway, italic text is found now as I’d expect, thanks.

I have not been able to find any app that would do what I need, Scrivener getting closest. Find by formatting seems like a very very special feature to me. Can you suggest any application that does this?

To recap, I need to search a document (RTF) and add a few special characters around italic text or bold text, like so [some italic text here], keeping the rest of the document intact and save it as RTF.

Keep with Next is a useful code that you can attach (via the Format/Text/Keep with Next menu command) to things like headings or on images prior to their captions. It ensures that if there is a page break slated to fall between it and the stuff following, it keeps the two things together instead of breaking the elements off between pages. Since it is an invisible code that is otherwise difficult to find, that is why we have a tool for it.

Well, there is the obvious, Microsoft Word. When the question is if there is software that does X (and it doesn’t really matter what X is, up to and including launching rovers to Mars) Word can probably do it. If you don’t have Word, try a free copy of LibreOffice. It has a comprehensive search and replace tool that includes formatting. You could do exactly what you are asking for with that. It’s a bit of a download, and it’s about as awkward and mammoth as Word, but if you want to find every single italic string of text and wrap it in some text, that’ll do it.

I’m not using and will not use Word. And I strongly dislike Libre Office - but have it. I had a look at its Find & Replace now and indeed formatting is there. But not surprisingly I can’t figure how to define character addition… As I don’t want to replace the italic text, just add something around it. Unless regular expressions would do it, of which I have no clue. I have the latest version of Libre Office. Sigh. I persist because it would save me a lot of time and errors to have this done automatically.

If you’re not financially too challenged, look at Nisus Writer Pro. You can do exactly what you want in the “Find & Replace” dialog, allowing you to search for a style, and to set up a replacement, not changing the style, but surrounding it by other text or symbols. Also:

1 NWP is completely Mac-like in its UI and has a fully-fledged style system.

2 NWP has regex and a less geeky way of using regex as well as normal Find & Replace. And having set up your F-&-R so it works the way you want, you can macro-ize it with a click of the mouse, save the macro and give it a keyboard shortcut, so it’s immediately accessible for any future use.

3 NWP uses RTF as its default format and handles RTFD perfectly, which makes it the perfect companion WP for Scrivener.

4 NWP can import and export DOCX if you need to collaborate with someone who insists on that format.

5 NWP has the best approach to creating shortcuts of any app I’ve ever used in 25 years of Mac use.

6 NWP has very powerful macro capabilities.

7 Nisus has a very good forum with members who are just as supportive as those here, including people who are expert in writing macros and often quickly write a macro to do the sort of thing you want to do on an appeal for help. And Martin of Nisus is extremely knowledgeable and very helpful.

Give it a try — it has a two-week trial period, I think — and educational discounts. But there will be a bit of a learning-curve to do what you want with F-&-R; but, as I say, you should get plenty of help from the forum.

Mr X

Disclaimer: I am not related to Nisus in any way, other than being a very happy user for many, many years.

xiamenese, thank you for the suggestion. I downloaded Nisus Writer Pro and will have a further look at it. I wouldn’t have other use for it though, I’m happy with Scrivener + Bean + Textwrangler combo.

You can do it with regular expressions. This works in OpenOffice on Windows, and I’m guessing that LibreOffice will be the same, but I’m not sure on all the distinctions between the two. See how it goes for you (on a test file!). Select that option and put your search term in rounded brackets:

(Search Term)

Set it to the italic format as usual. Then for your replacement, enter the characters you want to add, with $1 standing in for the search term text. So, e.g.

$1

When replaced that should give you

Search Term

If $1 isn’t working for the replacement with regex enabled, try \1.

Thank you MimeticMouton. \1 works for me BUT I can’t use (Search Term) because I need to add the characters around any italic text. Is there a regular expression that selects all italic text?

If I leave “Search for” field empty, everything will be replaced even though I have selected for italic formatting. See screenshot:

app.box.com/s/jpxvd9aqc5y4zdtv2qn3vri3ron4xfr8

“Italic, normal” is selected for search but Libre Office obviously doesn’t like mixing formatting and regular expressions. Selecting “Include Styles” makes no difference.

I don’t know if this makes sense but I tried for example this, and nothing happens:

app.box.com/s/ulcgjixmlcigl56jyy8lbi7fcr0jxbj8

Sorry if I’m missing something obvious. I’m not familiar with regular expressions or Libre Office and the like.

A regular expression is an advanced search syntax for finding text. It is wholly ignorant of things like formatting. You can’t search for italics with it, that is what LibreOffice is for. If you’ve ever used a search engine (such as the one on this forum) that lets you search for things like “compil*”, then you know the asterisk means: look for the string of letters ‘compil’ and let there also be matches that may have more letters after it, like ‘compiler’, ‘compile’ or ‘compiling’. Regular expressions are that concept on steroids.

For \1 to work, you need to store some search material into that token. And we do this by searching for stuff, and wrapping the part we want to store into \1, with parentheses. Here is a very simple example:

Libre(Office)

Now if we replace that with the following, we will get “OpenOffice”:

Open\1

The \1 recalls the stuff inside the parentheses and prints it as part of the replacement. You could search for “(last name), (first name)” and flip the order with “\2 \1”. Hopefully that all makes sense.

So what you want to do is find anything that is italic, and this is where the asterisk trick before, or the wildcard, comes into play. RegEx is full of wildcards, and variations on wildcards, and ways to use wildcards that even confuse programmers, but fortunately we just need a simple one. Since we don’t care what actual text we are finding is, and just care if it is italic, we can use a universal wildcard and specify that we don’t care how many letters and spaces and numbers and symbols we get, just so long as they are italic:

code
[/code]

The ‘.’ in regular expressions means any character, it’s a bit like the asterisk in simpler search syntaxes, but only one character. Absolutely anything, even stuff you can’t see, like tabs and spaces will qualify as a ‘.’. The ‘+’ is a quantifier, or how many of those we want. It means one or more of whatever falls before it, so in this case we want one or more characters of any kind that are italic. You’ll recognise that we then wrap it in parentheses to store what we capture into \1.

Now, you can put whatever you want around it in the replacement field, like so:

{{\1}}

In this sample paragraph, if we ran that search and replace, we would get:

In {{this sample paragraph}}, if we ran that search and replace…

Ha, thank you for adding all the explanation. :slight_smile:

I must be a complete idiot as I don’t get this to work. Where in (.+) is the italic?

(.+) for search and {{\1}} for replace will replace everything, naturally.

I think I’m giving up on Libre Office. I’m asking for something advanced and want it done dead simple. I do think that programmers are for thinking for the rest of us who just want their work done.

You still need to have the italics bit added to the find parameters, otherwise, like you say, that will match everything. You are combining two things together: (a) the limit to only find italic text and (b) the text to find and store into \1. If you leave (a) out you find all text, if you leave (b) out you store nothing into \1.

Well, hopeless this seems. I finally managed to select all other text except italic. No idea why. However adding negation ?! to the regex didn’t reverse the selection. Also, suddenly Libre Office stopped understanding \1 and started to understand $1. No idea why.

My simple solution to this challenge: open RTF in Textwrangler (so as plain text), search for tags for italics and replace them, save. No need for regular expressions.