EnhanceAnyLexer icon indicating copy to clipboard operation
EnhanceAnyLexer copied to clipboard

Highlighting a substring in a string

Open JoyHak opened this issue 11 months ago • 4 comments
trafficstars

I really liked the plugin, but I ran into its limitations. I need to highlight some words in specific strings, quotes, after certain commands, and so on. For example, I want to highlight word in quotes:

"higlight word and some word" 
"and word here" but not w0rd
and not word here   

If I write a regular RegEx word, it will highlight everything in a row. I can't highlight quotes only. If I add all existing styles and delimiters to excluded_styles, I will have to specify a whitelist for each color.

I came up with pure RegEx solution that highlights substrings with a condition:

(?: 
    ["']\K  	# Condition: quotes
)

|  		# Beginning of the loop

(?<=\G)  		# Ensures that the condition is found
[^"']*?  		# Quotes after which the entire regex stops
\K  			# Reset the match start again
\w+  		# Greedy: matches any words/numbers 

 (6) if condition is not found, alternation (?<=\G) is checked for each character; there is no skipping of unsuitable lines; flags do not work (*SKIP)(*F). Despite the fast speed of work, the number of steps tends to 100,000.

Please add the ability to use nested RegEx: the first one will search for a specific string, the second one will highlight characters/words in it, and so on. For example:

#fbcf20 = [regex, subregex]

JoyHak avatar Dec 11 '24 17:12 JoyHak

First of all, thank you for the interest in this plugin and the time you spent thinking about how to improve it. What do you think about defining, let's say, captures whose name would be freely selectable, in which you can then search again. Something along the lines of

capture_1 = regex
what_ever = capture_1 = a regular expression that searches within capture_1, but only if something was found
#fbcf20 = what_ever = the final regular expression that is only executed if what_ever has found something

This would have the advantage that the regular expression is easy to identify, as capture_1 and what_ever would have to be defined beforehand and capture_1 could be reused by several regular expressions. I am thinking for example of json or xml documents where the same nodes occur repeatedly.

Out of curiosity, which application is used in the “Mac screenshot”?

Ekopalypse avatar Dec 12 '24 16:12 Ekopalypse

This definition is very handy and practical, I like it!

However, RegEx has a problem that is invisible in NPP++, but visible on the regex101 website. My request to improve the plugin is not related to this problem. But it may show up after the update.

When the RegeEx engine searches for an capture_1 expression, it checks each character for a match. The more characters, the more steps it takes to check the truth of the expression (shortly steps). This mechanism only has an advantage if you need to find an expression somewhere in the text that needs to be fully parsed, for example: if (condition) {some text and capture_1}

In practice, there are cases where the exact position of the capture_1 we are looking for is known. For example, I know that the expression capture_1 is at the beginning of a line. So I write the expression ^capture_1, where the ^ symbol means the start of the line. However, the RegEx engine keeps comparing each character for the presence of the ^ anchor (it keeps looking from the beginning of the line to the end of the line regardless of anything).

This problem can freeze Notepad++ very quickly. The slightest error in an expression/unexpected case in a document will cause more then 100.00 steps in 1500+ lines document.

Theoretical solution: use hidden variable in the plugin's source code, assigned to the expression. It can be RegEx expression *SKIP *FAIL. It needs testing and I have not been able to get it to work correctly in all cases...

JoyHak avatar Dec 13 '24 09:12 JoyHak

For screenshots, I use the ShareX app with the theme. It gives you the ability to draw something on the screen before capturing an image (a mini photo editor with the abilities to blur, add text, ...). It also allows you to record GIFs and videos, but I highly recommend not using it for anything other than screenshots bc its buggy. For screenshots and videos, I can recommend IceCream

JoyHak avatar Dec 13 '24 09:12 JoyHak

Having some version of this would be very helpful. Ran into similar problems when switching over from EnhanceUDLLexer.py (which has stopped working properly for me for reasons I couldn't figure out). In that, we could specify which matching group of the regex to apply the color to, which took care of this type of issue (at least for my use cases; looking at JoyHak's comments, I'm not sure it would handle theirs easily).

So far I've been able to get my old styles working now that I've discovered how \K works (thank you for including that in your example; I likely would've missed that functionality entirely otherwise), but if I had any styles that used a matching group other than the final one, I don't think I could replicate them.

SnoringFrog avatar Feb 12 '25 21:02 SnoringFrog