Page 1 of 1

Support for \n and \1 in regular expressions

Posted: Mon Jun 02, 2008 8:37 am
by jelovirt
Currently when you use regular expressions in find/replace, you have to insert newline using Cmd + Enter or using the context menu. More or less all other regular expression languages accept newline as '\n'. Could this be added in a future release?

Also, as a personal preference, I'd like to use '\1' instead of '$1' for back references. If you try to use for example '\1' in the replacement text, the result is just '1'. Does this mean '\' + number is not used for anything in the regexp syntax and could be made a back reference construct?

Re: Support for \n and \1 in regular expressions

Posted: Mon Jun 02, 2008 12:03 pm
by sorin_ristache
jelovirt wrote:Currently when you use regular expressions in find/replace, you have to insert newline using Cmd + Enter or using the context menu.
I am sorry, you do not need Cmd + Enter in the Find/Replace dialog. \n is accepted as regular expression that matches a newline character if you select the Regular expression checkbox.
jelovirt wrote:Also, as a personal preference, I'd like to use '\1' instead of '$1' for back references. If you try to use for example '\1' in the replacement text, the result is just '1'. Does this mean '\' + number is not used for anything in the regexp syntax and could be made a back reference construct?
It means only that \1 does not have special meaning in a regular expression. The syntax of the regular expressions accepted in the Find/Replace dialog is the syntax of Perl 5 regular expressions which I think is kind of standard syntax for regular expressions. I do not know about any regexp language that specifies the substring of the first regexp group with \1 instead of $1. I think we should keep the standard regexp syntax that the users know from other regexp languages instead of inventing our own regexp language.


Regards,
Sorin

Re: Support for \n and \1 in regular expressions

Posted: Mon Jun 02, 2008 2:21 pm
by jelovirt
sorin wrote: I am sorry, you do not need Cmd + Enter in the Find/Replace dialog. \n is accepted as regular expression that matches a newline character if you select the Regular expression checkbox.
Yes, but only in find. If you try to use it in replace, e.g. in equivalet to Perl 's/, /\n/' for input 'foo, bar', you will get 'foonbar'.
sorin wrote: It means only that \1 does not have special meaning in a regular expression. The syntax of the regular expressions accepted in the Find/Replace dialog is the syntax of Perl 5 regular expressions which I think is kind of standard syntax for regular expressions. I do not know about any regexp language that specifies the substring of the first regexp group with \1 instead of $1. I think we should keep the standard regexp syntax that the users know from other regexp languages instead of inventing our own regexp language.
I totally agree. I just did a quick test and Perl 5 seems to support both '\1' and '$1'. Hadn't noticed that before. However, e.g. POSIX, Python, Ruby, and Java regexp use '\1'.

Re: Support for \n and \1 in regular expressions

Posted: Mon Jun 02, 2008 3:45 pm
by sorin_ristache
jelovirt wrote:Yes, but only in find. If you try to use it in replace, e.g. in equivalet to Perl 's/, /\n/' for input 'foo, bar', you will get 'foonbar'.
Yes, in the Replace field \n is not supported as the tooltip of that field specifies. We will consider allowing it in the Replace field too.
jelovirt wrote:I just did a quick test and Perl 5 seems to support both '\1' and '$1'. Hadn't noticed that before. However, e.g. POSIX, Python, Ruby, and Java regexp use '\1'.
I am sorry, Java regexp does not allow \1 instead of $1 as the first match group in the replace expression. I tested it with a small Java program. The Javadoc says:
Notable differences from Perl:
* In Perl, \1 through \9 are always interpreted as back references
Regards,
Sorin

Re: Support for \n and \1 in regular expressions

Posted: Mon Jun 02, 2008 4:16 pm
by jelovirt
sorin wrote:I am sorry, Java regexp does not allow \1 instead of $1 as the first match group in the replace expression.
My bad, I confused backreferences with group references. But in case, Perl does support '\1' and Python only uses it. So, if \n is not currently used for anything, then you could add it without breaking compatibility.

Re: Support for \n and \1 in regular expressions

Posted: Thu May 21, 2009 8:15 am
by jelovirt
Have you any plans on adding support for \n and \t in the search replace in the future releases?

Re: Support for \n and \1 in regular expressions

Posted: Thu May 21, 2009 3:31 pm
by Radu
Hello,

We will probably add support for replacing with \n and \t in a future version.

Regards,
Radu

Re: Support for \n and \1 in regular expressions

Posted: Mon May 25, 2009 11:49 am
by Radu
We have added support for replacing \n, \t and \0...\9. It will be available in the next minor version, 10.3.

Regards,
Radu