Find and replace — regular expressions

Does the Find & Replace dialog box allow backreferences in the
'Replace with' field?

(I'm having trouble getting it to work.)

unless I am reading this wrong - Yes!

Position yourself anywhere in a doc.

Find and Replace
Search for XXX
Replace with YYY
Press "Replace All"

All occurrences of XXX should be replaced by YYY

If you press "replace" it will only look forward and then ask you whether to check the rest of the doc.

Cheers

I think you read my question incorrectly.

Specifically, I am able to use regular expressions to search for a one
to many-digit number:
\d*
But I want to leave the number (whatever it may be) in place and
insert a tab immediately after it. I expected I should be able to use:
\1\t
but LO is interpreting the \1 *literally* instead of as a backreference.

:frowning:

[...]

I think you read my question incorrectly.

That's because your original question was not at all clear! (You owe it to correspondents not to waste their time.)

Specifically, I am able to use regular expressions to search for a one to many-digit number: \d*
But I want to leave the number (whatever it may be) in place and insert a tab immediately after it. I expected I should be able to use: \1\t but LO is interpreting the \1 *literally* instead of as a backreference.

There are two things wrong here, I think. First, although \1, \2, and so on can be used to refer back to parts of the "Search for" field *within the "Search for" field itself*, the corresponding syntax in the "Replace with" field is $1, $2, and so on. But then you also have to mark which part of the matched string you want to refer back to - and you do that using parentheses in the Search field.

If you want to refer to the entire matched string using $1, you must use (\d*) as your search string. But - easier than that - you can always refer to the entire matched string using the ampersand: & (or you can use $0). In that case the parentheses are unnecessary in the search string: you would search for \d* and replace with &\t.

I trust this helps.

Brian Barker

On Sun, Oct 27, 2013 at 7:39 PM, T. R. Valentine <trvalentine@gmail.com>wrote:

Does the Find & Replace dialog box allow backreferences in the
'Replace with' field?

(I'm having trouble getting it to work.)

Per the LO help, use $1 in the replace box, rather than \1.

Thanks, but this is removing the number. I want to retain the numbers.

In the LO help that I see, $ limits searches to items at end of
paragraphs. Where is the reference that it functions as a
backreference?

If you want to refer to the entire matched string using $1, you must use
(\d*) as your search string. But - easier than that - you can always refer
to the entire matched string using the ampersand: & (or you can use $0). In
that case the parentheses are unnecessary in the search string: you would
search for \d* and replace with &\t.

Perfect.

I trust this helps.

Indeed. Many thanks.

"T. R. Valentine"

Per the LO help, use $1 in the replace box, rather than \1.

You need to use '\N' for backreferences in the Search field.

It might be that help files are not updated yet.

Since 4.0, LibreOffice is using standard regexp library provided by ICU
project. You can use expressions listed at their webpage:
http://userguide.icu-project.org/strings/regexp
LO does not support "Flag Options".
Also, due to some bugs, not all of them work in Impress. But you are
safe in Writer in Calc.

I disagree. The question was perfectly clear. "Backreference" is
one of basic terms in regular expressions theory. If you ask about
regexpes, you may safely assume that your peers knows what
"backreference" means.
In the same way, when you ask about databases, you don't explain what
SQL is.

Statement in parentheses applies to both sides - the one asking the
question as well as people who answer it.

Dnia 2013-10-28, o godz. 00:24:38 Brian Barker napisal(a):

That's because your original question was not at all clear! (You owe it to correspondents not to waste their time.)

I disagree. The question was perfectly clear.

Thanks for this - but I'm afraid not. The original question was "Does the Find & Replace dialog box allow backreferences in the 'Replace with' field? (I'm having trouble getting it to work.)". No-one was able to answer it until the original questioner explained himself. Were you perhaps instead reading his revised message?

"Backreference" is one of basic terms in regular expressions theory. If you ask about regexpes, you may safely assume that your peers knows what "backreference" means.

You've latched onto "backreference" as if not understanding that was the substance of my comment. But I didn't mention that. Instead, the problem was that the questioner initially did not explain what problem he had with backreferences. Indeed, it transpired that his problem was that he didn't need backreferences at all. Backreferences are how you refer back to a matched string within the pattern itself, not how you refer to the same thing in the replacement string. (See Wikipedia: "grouping subexpressions with parentheses and recalling the value they match *in the same expression* (backreferences)" (my emphasis).) The user's problem was that he was erroneously using the syntax for backreferences in an attempt to create a reference in the replacement string.

Statement in parentheses applies to both sides - the one asking the question as well as people who answer it.

Well, that's certainly true. But I cannot see how you can suggest (as you clearly were) that I was wasting the questioner's time. Once he identified his problem, I provided an explanation which he described as "Perfect".

Brian Barker

Hi :slight_smile:
Questions are often vague. Even where they are clear it's often
obvious the person asking the question really needs to know something
they had no idea how to ask about. Either a shortcut route or a
"don't even try it , do this instead". Often 3 different people can
read the same question and have 5 different ideas about how best to
answer it.

It's pretty rare for anyone to acknowledge when a question has been
solved and a "Perfect" is almost unheard of even though many of you do
give excellent answers.
Regards from
Tom :slight_smile: