Search & Replace in the middle of complex line

Tool for Search and Replace across multiple files.
User avatar
Me & Myself

Search & Replace in the middle of complex line

Post by Me & Myself »

Hi, I want to create a batch in TextCravler to search in lines like this one:
[CT_Combattest6]Koадa будe>e аo>oвы, =aк_и>e <COMMAND_KICK> т>oбы =aтa>ь. Сoкaлуйc>a, вoopуки>ecь <COLOR,255,155,155>Сиc>oлe>o_<CLEARCOLOR> для cлeду^щeй cxвa>+и.
[CT_Combattest9]<COLOR,255,155,155>Tec> зaвep%e=:<CLEARCOLOR> Кди>e у+aзa=ий.
[CT_reset1]<COLOR,255,155,155>Cбpoc тepeз 30 ce+у=д...<CLEARCOLOR>
[CT_Navtest3]Bы _oкe>e вый>и, +oадa будe>e аo>oвы.
and replace E with A for example
It has to search only in the text and skip [...] and <...> tags. btw there is also a > char in the text that will be used

So far i made this regex, to skip the [...], but it works inly with the last line
search: (\[.+\].*)E
replace: $1A
thanks
User avatar
silentguy

Re: Search & Replace in the middle of complex line

Post by silentguy »

Okay, multiple points. First of all, try to avoid . if you want something followed by something else because . is greedy, So, searching for "(\[.+\].*)" in "[a] asdfasdfsadf " the .+ part would grab the whole "a] asdfasdfsadf [b" part.
"(\[[^]]+\].*)" in this instance would be better because it it does not allow "anything" between the [] but only allows "anything that's not ]" :)

But that was just a general comment about regexp. Now to you actual question.
I guess I would go with

Code: Select all

(?<! //the stuff in this brackets should NOT be before my searched phrase
\[[^]]*      // [ without a matching ]
|             // or 
<[^>]*     // < without a matching >
)            //here ends the unwanted stuff
E           //what i actually search
regexp can't really count brackets, therefore this would for instance make mistakes on "[bla [blub] E ]" because it would "use" the first ] on both [, but your text does not seem to have nested brackets, therefore it would work
User avatar
Me & Myself

Re: Search & Replace in the middle of complex line

Post by Me & Myself »

Thank you very much. This is so alien to me, but its working like magic :)
User avatar
silentguy

Re: Search & Replace in the middle of complex line

Post by silentguy »

Always happy to help. I've never really had to solve anything complicated with regexp, but I just like doing it cause I like logic riddles. :-D
Post Reply