Reg Ex Captures

Reg Ex captures appear to be not working in Version: 24.2.4.2 (X86_64).

Ampersand(&) works but &1, &2 etc. don’t.

Am I missing something simple?

Please give more details. Could you give an example of a failing regexp? Just to make sure, have you ticked the Regular expressions check box?

I have a line of text:
What is Sticky

In Find and Replace I type “S(.*)y” in the Find box and “&1” in the Replace box.

I expect the line to change to “What is tick”

Instead, I get “What is Sticky1”

Yes, the Regular Expressions box is checked.

from List of Regular Expressions :

and

2 Likes

“What is Sticky” with S(.*)y&1 ==> “What is Sticky1”

What you get is expected.

  • According to built-in help & captures the total matched expression in the Find box, i.e. what corresponds to S(.*)y = “Sticky”. This is the first component in your Replace box.

  • You then have a literal “1” in the Replace box which suffixed to the match.

The result is “Sticky1”.

Capture groups, delimited by parentheses, are named $n where n is the sequence number of the left parenthesis, starting from 1. Your correct Replace expression would be $1.

Thank you. I just hadn’t taken in that the symbol is ‘$’. I’m used to “\1”, “\2” etc. Symbol “$” I thought had only one use - end of line.

Reg Exp are very powerful but sometimes very tricky.

Thanks, Ajlittoz.

–Greg

Regular expressions are not that “regular”. At least, there is ambiguity in the wording. One must be very careful about the use of meta-characters: their meaning changes between Find and Replace boxes. As en example, $ is end-of-paragraph in Find and capture substitution sentinel (needing an argument) in Replace.

if so, please report !

latest known was tacked here : Bug 153212 - “regular expression in the Replace box” makes no sense :innocent:

I can’t report against LO. The ambiguity is in the “historical” specification of regexp. Changing for a more “normalised” vocabulary would require to change nearly all IT literature. So we have to live with it.

$1, $2, $3 do work.