Support for \n and \1 in regular expressions

Are you missing a feature? Request its implementation here.
jelovirt
Posts: 84
Joined: Tue Oct 11, 2005 9:17 am
Location: Helsinki, Finland
Contact:

Support for \n and \1 in regular expressions

Post 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?
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

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

Post 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
jelovirt
Posts: 84
Joined: Tue Oct 11, 2005 9:17 am
Location: Helsinki, Finland
Contact:

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

Post 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'.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

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

Post 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
jelovirt
Posts: 84
Joined: Tue Oct 11, 2005 9:17 am
Location: Helsinki, Finland
Contact:

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

Post 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.
jelovirt
Posts: 84
Joined: Tue Oct 11, 2005 9:17 am
Location: Helsinki, Finland
Contact:

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

Post by jelovirt »

Have you any plans on adding support for \n and \t in the search replace in the future releases?
Radu
Posts: 9057
Joined: Fri Jul 09, 2004 5:18 pm

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

Post by Radu »

Hello,

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

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Radu
Posts: 9057
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply