RegEx: match a specific string that is not inside the HTML tag

<tag value='botafogo'> botafogo is the best </tag> 

A match is required only with botafogo (... is the best) and not the value of botafogo

my program automatically annotates the term in clear text:

 botafogo is the best to <team attr='best'>botafogo</team> is the best 

and when I "replaced everything" with the "best" word, I have a big problem ...

 <team attr='<adjective>best</adjective>'>botafogo</team> is the <adjective>best</adjective> 

Ps .: Java language

+4
source share
5 answers

The best way to achieve this is to NOT use a regular expression and use the correct HTML parser. HTML is not an ordinary language, and doing this with a regular expression will be tedious, difficult to maintain, and most likely still contains various errors.

HTML parsers, on hand, are good for working. Many of them are mature and reliable, and they take care of every little thing for you and make your life easier.

+5
source

Do you think DOM functions are used instead of regular expression functions?

 document.getElementsByTagName('tag')[0].innerHTML.match('botafogo') 
+4
source

An HTML parser is best, and then loop through text content. (See Other Answers.)

If you're in PHP, you can make a quick decision by doing strip_tags() in the content to remove the HTML first. It depends on if you are performing a replacement, in which case deletion is not an option at first, or if you just match, in which case content that is not part of the match can be deleted without problems.

+1
source

@OP, in your favorite language, break down into </tag> , then do another split into > . e.g. python

 >>> s="<tag value='botafogo'> botafogo is the best </tag>" >>> for item in s.split("</tag>"): ... if "<tag" in item: ... print item.split(">")[-1] ... botafogo is the best 

No need for regular expression

0
source

I just looked for a solution to one problem and created one that seems to do the job.

A negative look is the key. To make sure the match is not in the tag, look ahead to make sure that the bracket of the closing corner is not found before opening. Suppose we want to find the word "needle":

 #needle(?![^<]+>)#i 

My business is in PHP, and it looks something like this:

 function filter_highlighter($content) { $patterns = array( '#needle(?![^<]+>)#i', '#<b>Need</b>le#', '#<strong>Need</strong>le#' ); $replacement = '<span class="highlighted">Need</span>le'; $content = preg_replace( $patterns, $replacement, $content); return $content; } 

While this is working.

0
source

Source: https://habr.com/ru/post/1302942/


All Articles