Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the regular expression replacement character #2439

Open
gunchleoc opened this issue Feb 16, 2020 · 5 comments
Open

Document the regular expression replacement character #2439

gunchleoc opened this issue Feb 16, 2020 · 5 comments

Comments

@gunchleoc
Copy link

gunchleoc commented Feb 16, 2020

It took me quite a while to guess that the replacement character for Search & Replace is not $1 or \\1 but \1. STFW came up empty, since most questions are about problems with the pattern, not with the replacement/substitution character.

I think this should be documented somewhere on https://www.geany.org/manual/gtk/glib/glib-regex-syntax.html or <local_path>/doc/geany/html/index.html#regular-expressions.

@codebrainz
Copy link
Member

It is documented in the manual:

\n Where n is 1 through 9 refers to the first through ninth tagged region when searching or replacing. Searching for (Wiki)\1 matches WikiWiki. If the search string was Fred([1-9])XXX and the replace string was Sam\1YYY, when applied to Fred2XXX this would generate Sam2YYY.

Maybe it needs to be called-out more, since the GLib docs don't do a good job either.

@elextr
Copy link
Member

elextr commented Feb 17, 2020

Maybe it needs to be called-out more, since the GLib docs don't do a good job either.

Yeah, glib documents it on the replace function https://developer.gnome.org/glib/stable/glib-Perl-compatible-regular-expressions.html#g-regex-replace not in the regex syntax section so its not that obvious.

@gunchleoc
Copy link
Author

Thanks for pointing that out. I did not consider "replace" or "replacing" as a search keyword - I was looking for "replacement" and "substitution".

How about splitting the list up into characters used in the match, and characters used in the replacement, with separate headers?

@cousteaulecommandant
Copy link
Contributor

May I also suggest replacing \n with \1...\9? Maybe that way it is easier to visually identify if you're looking for it, and less ambiguous (a literal \n means newline, and maybe someone reading the docs quickly just ignores that entry because they assume it's talking about the newline).

(And maybe it is a good idea to add a note in the documentation highlighting that & and $1...$9 don't work as they do in other regex "dialects" such as Perl; it's \0 and \1...\9 here. After all, the documentation does claim that "the syntax is Perl compatible", so one might assume this also applies to substitution patterns.)

@elextr
Copy link
Member

elextr commented Feb 25, 2020

It may be a little subtle, but the regex syntax is Perl compatible, (and it is also yuckmascript compatible if a flag is set, Geany doesn't set it) but replacement isn't part of the regex syntax and thats why it isn't documented in the Glib regex syntax page.

My reference to Glib's replace is irrelevant, it appears that the replace is Geany's own so its totally unrelated to any Perls, so yeah, it should be clearly documented in Geany docs.

Why the Geany docs repeat the regex stuff I'm not sure, but I think its from back when it used a less common regex engine, but thats where the replace stuff is hidden, maybe it should be here.

Pull requests are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants