1

I have these 2 html tags:

<title>The Secret Is Elsewhere</title>

<meta name="keywords" content="love, find, car, diamond"/>

With the regex below I can replace the content of the <title></title> tag with the content of <meta name=..> tag

. matches newline:

Search: (<title>(.*?)<\/title>.*?)(<meta name="keywords" content=").*?("\/>)

REPLACE BY: \1\3\2\4

BUT, I need to put a comma between words, after replace, on the ` tag

So, the output should be:

<meta name="keywords" content="the, secret, is, elsewhere"/>

Can anyone help me?

2 Answers 2

2
  • Ctrl+H
  • Find what: (<title>(\S+)(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?</title>[\s\S]+?<meta name="keywords" content=")[^"]+
  • Replace with: $1(?2$2)(?3,$3)(?4,$4)(?5,$5)(?6,$6)(?7,$7)(?8,$8)(?9,$9)(?10,$10)(?11,$11)(?12,$12(?13,$13)(?14,$14)
  • UNCHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(                   # start group 1
   <title>            # literally, open tag
     (\S+)              # group 2, 1 or more non-space
     (\s+\S+)?          # group 3, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 4, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 5, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 6, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 7, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 8, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 9, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 10, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 11, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 12, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 13, 1 or more space followed by 1 or more non-space
     (\s+\S+)?          # group 14, 1 or more space followed by 1 or more non-space
   </title>           # end tag
   [\s\S]+?           # 1 or more any character, including newline
   <meta name="keywords" content="      # literally
)                   # end group 1
[^"]+               # 1 or more any character that is not a quote

Note: This is working for up to 13 words, you can add as many groups as needed if you have more than 13 words

Replacement:

$1              # content of group 1
(?2$2)          # if group 2 exists, insert it
(?3,$3)         # if group 3 exists, insert a comma then content of group 3
(?4,$4)         # idem for group 4
(?5,$5)         # idem for group 5
(?6,$6)         # idem for group 6
(?7,$7)         # idem for group 7
(?8,$8)         # idem for group 8
(?9,$9)         # idem for group 9
(?10,$10)       # idem for group 10
(?11,$11)       # idem for group 11
(?12,$12)       # idem for group 12
etc. 

Note: Add other groups if needed.

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

8
  • this doesn't work. If I have <title>I love myself because it is nice to love | My Name (en)</title> then your regex will not work. Or if <meta tag..> has more then 7-8 words. Must be a general regex, for all king of words and symbols on the tags.
    – Just Me
    Commented Aug 5, 2020 at 12:56
  • 1
    @JustMe: As I said in my answer, you can add as many groups as needed. Here I give an expression for 1 up to 6 words. Simply add (\s+\S+)? 10 or 20 times in the regex and add (?n,$n) in the replacement, n varying from 2 upto 10 or 20. If you have 20 groups, the regex will work for 1 word upto 19 words without any modification.
    – Toto
    Commented Aug 5, 2020 at 14:17
  • @JustMe: I don't think there is another way to do the job in Notepad++, but this can be easily done with a small script, written in Perl for example.
    – Toto
    Commented Aug 5, 2020 at 14:20
  • can be done, a little harder. After replace with my regex, must use another regex: SEARCH: (content=")(.*?(?<!,))\x20\|?\x20*(.*) REPLACE BY $1\l$2,\x20\l$3 You must do a multiples replace, and it is so fine. But, as I say, it take more time
    – Just Me
    Commented Aug 5, 2020 at 15:01
  • 1
    @JustMe: I've explained in my first comment above what to change in the regex for let it work in all cases, just add max number of groups to match the longest string of words and it will work for any number of words.
    – Toto
    Commented Aug 5, 2020 at 16:19
0

you can find another great answer here:

https://community.notepad-plus-plus.org/topic/19806/regex-put-a-comma-on-replace-html-tags

1
  • Link only answer is useless, especially when it will be broken. Can you elaborate on this a little more?
    – Toto
    Commented Aug 13, 2020 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .