0

I have this html tag (from string):

<meta name="description" content="I love my mother" but I love my sister" more than I can say"/>

As you can see, I have 4 double quote (apostrophe) in the content section. Should be only 2 double quote: one at the beginning content=" and one at the end "/>

I must find all tags that contains other double quote except those 2 in the content section, and delete them:

Output should be:

<meta name="description" content="I love my mother but I love my sister more than I can say"/>

I made a Regex, but not too good. Maybe you can help me:

FIND: (?-s)(<meta name="description" content=")(*?\K.*"(?s))"/>

REPLACE BY: \1\2

1 Answer 1

1

Here is a way to go:

  • Ctrl+H
  • Find what: (?:<meta name="description" content="|\G(?!^))[^"]*\K"(?=.*?"/>)
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?:             # non capture group
  <meta name="description" content="      # literally
 |               # OR
  \G(?!^)         # restart from last match position (not at the beginning of a line)
)               # end group
[^"]*           # 0 or more non quote
\K              # forget all we have seen until this position
"               # a double quote
(?=.*?"/>)      # positive lookahead, make sure we have "/>  somewhere after

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Demo & explanation

4
  • thanks again @Toto
    – user706401
    Commented Apr 23, 2021 at 15:04
  • @RobRob: You're welcome, glad it helps.
    – Toto
    Commented Apr 23, 2021 at 15:05
  • can you please tell me what does this (?: non-capture group doing in your regex? This I do not understand very good.
    – user706401
    Commented Apr 23, 2021 at 20:08
  • 1
    @RobRob: (?: ... ) acts as a group ( ... ) but doesn't capture the matched data. It's really much more efficient than a standard capture group. It is use when you want to group something but don't need to reuse it later. See: regular-expressions.info/brackets.html
    – Toto
    Commented Apr 24, 2021 at 7:59

You must log in to answer this question.