0

I am using Regex (.*BankName.*>)(.*)(</.*) for finding & replacing it with \1\2,HDFC\3 in a file containing text as shown below:

<BankName>ICICI</BankName></not>

<BankName>ICICI</BankName>
</not>

when I click 'replace all', I get something like this in file:

<BankName>ICICI</BankName>,HDFC</not>


<BankName>ICICI,HDFC</BankName>
</not>

I am not able to understand the behaviour for the first line, why is it not placing ",HDFC" within BankName tag. Below are attached screenshot if Anyone needs them.

Before Pressing "Replace all": enter image description here

After Pressing "Replace all": enter image description here

Few More examples:

Before "Replace all":

<BankName>ICICI</BankName></not>


<BankName>ICICI</BankName>
</not>


<visibility name="BankName">ICICI</visibility></not>

<visibility name="BankName">ICICI</visibility>
</not>

After "Replace all": ICICI,HDFC

<BankName>ICICI,HDFC</BankName>
</not>


<visibility name="BankName">ICICI</visibility>,HDFC</not>

<visibility name="BankName">ICICI,HDFC</visibility>
</not>

I have built the regex i.e.(.*BankName.*>)(.*)(</.*) keeping in mind the data shown above.

ASharma7
  • 726
  • 3
  • 8
  • 27
  • Using a parser might be a better approach.I think looking into greedy vs non greedy can also be helpful. Are you looking for this? `(BankName[^\r\n<>]*>)(.*?)([^<>]*>` https://regex101.com/r/oeZeza/1 – The fourth bird Apr 20 '20 at 18:14
  • This regex did worked. But what you said completely went out of my head. – ASharma7 Apr 20 '20 at 18:20
  • Can you put this in answer, so that I can accept this as answer. – ASharma7 Apr 20 '20 at 18:22
  • 1
    [This page](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) has a nice explanation why using a parser is better. This page explains greedy and lazy https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions. The solution for this problem is a duplicate of https://stackoverflow.com/questions/7014903/my-regular-expression-matches-too-much-how-can-i-tell-it-to-match-the-smallest – The fourth bird Apr 20 '20 at 18:25

0 Answers0