[tw5] Re: Regex Character Classes not working in search-replace
Awesome, thanks. I had tried that but messed up the search-replace syntax, I didn't realize the angle brackets would replace the square brackets and was using both: [search-replace:i:regexp[],[$2, $1]] Much appreciated. On Saturday, August 7, 2021 at 3:42:06 AM UTC-4 saq.i...@gmail.com wrote: > It works fine with the shortcut class \w, but if I replace that with >> [a-zA-Z0-9_] it breaks, when logically those should be the same. > > > You cannot have the characters [ and ] inside a literal operand to a > filter operator. > The workaround is to define the regular expression as a variable and then > use the variable as the operand. > > Pseudo code below. > > \define myregexp() ((?:.*\s)|^)([a-zA-Z0-9_]+$) > > \define compare-by-last-name-with-character-class() > [search-replace:i:regexp,[$2, $1]] > \end > > -- You received this message because you are subscribed to the Google Groups "TiddlyWiki" group. To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/482ec638-e4fb-4e6f-a93c-e71924cbea04n%40googlegroups.com.
[tw5] Re: Regex Character Classes not working in search-replace
On Saturday, August 7, 2021 at 12:24:28 AM UTC-7 john@gmail.com wrote: > I'm using a macro and search-replace to convert a list of FirstName > LastName to LastName, FirstName. It works fine with the shortcut class \w, > but if I replace that with [a-zA-Z0-9_] it breaks, when logically those > should be the same. The problem is that *regex syntax uses square brackets* (to specify character classes), and *TiddlyWiki filter syntax also uses square brackets* (to specify literal text operands), and* the filter parser does not allow nesting of brackets*. Thus, when you use the character class regex syntax, it breaks the filter syntax due to nesting of square brackets. This is noted here: https://tiddlywiki.com/#regexp%20Operator, where it says: *The filter syntax makes it impossible to directly specify a regular expression that contains square brackets. The solution is to store the expression in a variable. * The workaround is to put your regex patterns into variables, and then use those variables in the filter, like this: \define compare-by-last-name-with-character-class() <$vars search="((?:.*\s)|^)([a-zA-Z0-9_]+$)" replace="$2, $1"> [search-replace:i:regexp,] \end Note that is this particular use-case, it's not strictly necessary to put the "replace" pattern into a variable, since it doesn't actually use square brackets in the syntax. However, I find that putting both the search pattern and the replacement pattern into variables makes the filter syntax more consistent and easier to read. enjoy, -e -- You received this message because you are subscribed to the Google Groups "TiddlyWiki" group. To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/067903e8-971c-480c-bcc9-7fc6d1a1ac1en%40googlegroups.com.
[tw5] Re: Regex Character Classes not working in search-replace
> > It works fine with the shortcut class \w, but if I replace that with > [a-zA-Z0-9_] it breaks, when logically those should be the same. You cannot have the characters [ and ] inside a literal operand to a filter operator. The workaround is to define the regular expression as a variable and then use the variable as the operand. Pseudo code below. \define myregexp() ((?:.*\s)|^)([a-zA-Z0-9_]+$) \define compare-by-last-name-with-character-class() [search-replace:i:regexp,[$2, $1]] \end -- You received this message because you are subscribed to the Google Groups "TiddlyWiki" group. To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/d1de53a2-5e56-4d28-be4b-88395db2f76cn%40googlegroups.com.