Bug 473266

Summary: Regex doesn't seem to respect flag state
Product: [Applications] kate Reporter: reptillia39
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: CONFIRMED ---    
Severity: normal CC: avollbracht, christoph, kare.sars
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Microsoft Windows   
OS: All   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description reptillia39 2023-08-10 22:51:17 UTC
SUMMARY
***
There seem to be a issue highlighting text when the regex contains a flag. (?s) is a flag in regex. That's where the issues comes in.
***

This is the text test.
```
fill "y+x*h" mirror. x

    m "rep_mitchell_concatenation_arguments_for_shapes:
     
        skip $""{1=1},$""{2=0},$""{3=1},$""{4=1}
     
        ratio:=cut($""1,0,1)
        if !$ratio error ratio_arg>0==F fi
        sublevel:=abs($""3)+1
        status $ratio,$""2,$sublevel,$""4
        "

    command "+rep_mitchell_concatenation_create_shape:
        skip $""{1=},$""{9=},$""{10=},$""{11=},$""{12=},$""{13=},$""{14=},$""{15=},$""{16=},$""{17=},$""{18=},$""{19=}
        sid,pixel_size,subpixel_level,ratio,angle,reflect_direction,sublevel,fit_inside=$""{1-8}
      
        if $pixel_size<4 error dim_form>=4=F fi
      
        if $_rep_mitchell_concatenation_use_shape_image
         
            $_rep_mitchell_concatenation_shape_image 
            
            _rep_mitchell_concatenation_use_shape_image,_rep_mitchell_concatenation_shape_image=
         
            if d!=1 error d_form_img==1=F fi
         
            if s<=4&&s>1
                if s==3
                    to_gray.
                else
                    s. c,{if(s==4,-3,if(s==2,-1,-s))}
                    to_gray..
                    *[-2,-1]
                fi
            elif s==5
                s. c,-4 cmyk2rgb.. to_gray.. *[-2,-1]
            fi
        else
            if narg($sid)
                if isnum($sid) if isint($sid)
                    sid=${arg\ $sid+1,australia,barbedwire,circle,crosshair,cupid,diamond,dragon,dragonfly,fern,flip,gear,gumleaf,heart,information,kookaburra,mail,mapleleaf,paint_splat,paw,phone,polygon,rooster,shopping_cart,snowflake,star}
                fi fi
            else 
                sid=diamond
            fi
         
            if narg($""{9--1}) 
                shape_$sid {$pixel_size*$subpixel_level},$""{9--1}
            else 
                shape_$sid {$pixel_size*$subpixel_level}
            fi
         
            resize. {ceil([w,h]/$pixel_size)*$pixel_size},100%,100%,0,0,.5,.5
        fi
      
        if $angle-360*floor($angle/360) rotate. $angle,1 fi
      
        if $reflect_direction==1 mirror. x
        elif $reflect_direction==2 mirror. y
        fi
      
        n. 0,1 autocrop. 0
      
        if $fit_inside
            resize2din. {vector2(round($pixel_size)*$ratio)},5
        else
            resize. {vector2(round($pixel_size)*$ratio)},100%,100%,5
        fi
      
        resize. $pixel_size,$pixel_size,100%,100%,0,0,.5,.5
      
        cut. 0,1 n. 0,1
        "

jp " " "" "" ""
```
This would be the regex to test
```
((?<=m )|(?<=command ))(?s)"{1}(.*?)"(?=\s)
```

STEPS TO REPRODUCE
1. Test the text test on regex101 website, and the regex on KDE. Then do the same on Kate.

OBSERVED RESULT
Kate fails to highlight texts which are highlighted in regex101/

EXPECTED RESULT
Kate should successfully highlight texts.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 reptillia39 2023-08-10 23:04:03 UTC
New Information. Even syntax highlighting approach does not work at all:

```
        <contexts>
            
            <context name="Multi-line Comment" attribute="Comment" lineEndContext="#stay">
                <RegExpr attribute="Quotation Mark" context="#stay" String="((?&lt;=m )|(?&lt;=command ))(?s)&quot;{1}(.*?)&quot;(?=\s)" />
            </context>
            
        </contexts>
        
        <itemDatas>
            <itemData name="Quotation Mark" defStyleNum="dsString" />
        </itemDatas>
```
Comment 2 Kåre Särs 2023-08-11 18:09:26 UTC
Thanks for the report!

I think the problem might be that Kate treats every line as a separate string. Only if the regular expression includes "\n", the search will search in a copy of all lines joined into one string with "\n".

It could be interesting to test if this could be fixed  by also treating the (?s) flags as a trigger to join the lines.

This would have to be checked/fixed both in the KTextEditor search and the Kate multi-file search plugin 

(this does go into the more advanced regular expressions :)
Comment 3 Kåre Särs 2023-08-11 18:33:04 UTC
Sorry not the string "\n" but the new-line character '\n' is used to join lines in multi-line searches :)
Comment 4 Vollbracht 2025-11-01 12:28:39 UTC
In general kate seems to fail in syntax highlighting when stumbling over a string containing uneven marks. Let's view some Expl3 code (LaTeX):

\exp_args:Nno \regex_replace_once:nnN { \(\s*[^\)]*\) } { \l_tmpb_tl } \l_tmpd_tl

\( or \) are escaped parentheses in second param (a RegEx) and should be invisible to syntax analysis. In this case I'm searching for an opening parenthesis followed by everything that is no closing parenthesis followed by the closing parenthesis. Syntax analysis however doesn't see "I'm searching for" or "no". It just sees "opening parenthesis" ... "closing parenthesis" ... "closing parenthesis" (uneven).

Is this still the same bug or is it to be filed new?