Regex Ignore text in square brackets

Hello Community,
I need help in creating this regex for replacing the text

Heres the sample input:

<variable class="loves">[loves] My dog loves dog food </variable>

Heres the sample output i am expecting:

<variable class="loves">[loves] My dog hates dog food </variable>

The regex i am currently using highlights the word loves written in square brackets as well as outside the square bracket. I want to ignore the word loves written in square bracket

Thank you

Hi Hansen Lobo
try this
(?<=dog).*(?=dog)
hope this helps
thanks

take
assign activity and name a variable/argument then
System.Text.RegularExpressions.Regex.Match(text variable,“(?<=dog).*(?=dog)”).Value.Trim

grafik

It works, But i wont be knowing the previous and next text. So other times it wont work Its dog in this case now. Also need to make sure i am editing texts with variable texts only
Lets try with some other examples
Input:

<variable class="loves">[loves] My dog loves dog food </variable>
<variable class="loves">[loves] My *cat* loves **dog** food </variable>
<li class="loves">[loves] My dog loves dog food </li>

Output

<variable class="loves">[loves] My dog hates dog food </variable>
<variable class="loves">[loves] My *cat* hates**dog** food </variable>
<li class="loves">[loves] My dog loves dog food </li>

Can you try this?

(?<=\>)\[.*\].*(loves).*(?=\<)

Hello Peter,

There are some cases, where there’s no space
Input
<variable class="loves">[loves]loves dog food </variable>
Output
<variable class="loves">[loves]hates dog food </variable>

then list all samples/variations, so we can check for the individual specifics

Hi,

How about negative lookahead and negative lookbehind as the following?

System.Text.RegularExpressions.Regex.Replace(yourString,"(?<![\[""])loves(?![\]""])","hates")

Regards,

1 Like

Input

<variable class="loves">[loves] My dog loves dog food </variable>
<variable class="loves">[loves] My cat loves dog food </variable>
<variable class="loves">loves dog food </variable>
<variable class="loves">[loves]dog loves</variable>
<variable class="loves">[loves]loves</variable>
<variable class="loves">dog loves</variable>
<variable class="loves">loves</variable>
<variable class="loves"> loves </variable>
<li class="loves">[loves] My dog loves dog food </li>

Output:

<variable class="loves">[loves] My dog hates dog food </variable>
<variable class="loves">[loves] My cat hates dog food </variable>
<variable class="loves">hates dog food </variable>
<variable class="loves">[loves]dog hates</variable>
<variable class="loves">[loves]hates</variable>
<variable class="loves">dog hates</variable>
<variable class="loves">hates</variable>
<variable class="loves"> hates </variable>
<li class="loves">[loves] My dog loves dog food </li>

With this expression:

(?<=\>)\[?.*\]?(loves).*(?=\<)

You should normally get all matches:
image

1 Like

It works!! Thank you !!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.