Biplov Dahal
Biplov Dahal

Reputation: 47

concatenate next to string if string found

I'm trying to search through my strings then concatenate the string.

string =    ' <html><body><svg style="background: #40484b;" version="1.1" viewbox="0 0 400 150" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
    <!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
    <rect height="100" id="path-1" width="100" x="25" y="25"></rect>
    <rect height="100" id="path-3" width="100" x="150" y="25"></rect>
    <rect height="100" id="path-5" width="100" x="275" y="25"></rect>
    <!-- gradient -->
    <lineargradient id="gradient" x1="50%" x2="50%" y1="0%" y2="100%">
    <stop offset="0%" stop-color="#FF8D77"></stop>
    <stop offset="50%" stop-color="#FFBDB1"></stop>
    <stop offset="100%" stop-color="#F4E3F6"></stop>
    </lineargradient>
    <!-- clip-path -->
    <clippath id="clip">
    <circle cx="325" cy="75" r="50"></circle>
    </clippath>
    <!-- filter -->
    </defs>>
    <g fill-rule="evenodd">
    <use fill="#FF8D77" fill-opacity="0.5" filter="url(#glow)" xlink:href="#path-1"></use>
    <use fill="url(#gradient)" xlink:href="#path-3"></use>
    <use clip-path="url(#clip)" fill="#FF8D77" xlink:href="#path-5"></use>
    </g>
    </svg>
    </body></html>'

That's how my string looks like. So far this is what I have

    string_list = string
    if "<defs>" in string:
        ###I'm trying to concatenate strings after <defs> like <defs> string string

Also, I'd like to keep appending strings instead of replacing. Like If I have a user type in something; it keeps adding strings after defs instead of replacing what I already have.

The output I'm expecting would be something like this, I added "concatenated string1" and "concatenated string2". I'm trying to add strings next to "defs" from my string list overall.

Also what I meant by keep adding strings instead of replace is for instance. I have a - first user type in string "concatenate1" - Second user type in string "concatenate2" I want to add both concatenate 1 + contatenate 2 next to my defs tag.

<html><body><svg style="background: #40484b;" version="1.1" viewbox="0 0 400 150" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs> <concatenated string1> <concatenated string2>
<!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
<rect height="100" id="path-1" width="100" x="25" y="25"></rect>
<rect height="100" id="path-3" width="100" x="150" y="25"></rect>
<rect height="100" id="path-5" width="100" x="275" y="25"></rect>
<!-- gradient -->
<lineargradient id="gradient" x1="50%" x2="50%" y1="0%" y2="100%">
<stop offset="0%" stop-color="#FF8D77"></stop>
<stop offset="50%" stop-color="#FFBDB1"></stop>
<stop offset="100%" stop-color="#F4E3F6"></stop>
</lineargradient>
<!-- clip-path -->
<clippath id="clip">
<circle cx="325" cy="75" r="50"></circle>
</clippath>
<!-- filter -->
</defs>>
<g fill-rule="evenodd">
<use fill="#FF8D77" fill-opacity="0.5" filter="url(#glow)" xlink:href="#path-1"></use>
<use fill="url(#gradient)" xlink:href="#path-3"></use>
<use clip-path="url(#clip)" fill="#FF8D77" xlink:href="#path-5"></use>
</g>
</svg>
</body></html>

Upvotes: 0

Views: 78

Answers (3)

Caleb
Caleb

Reputation: 2298

To insert additional string in your string variable html, you can do something like this:

import re
html = '<html> ... <defs><-- inserts will go here --> ... </html>'
insert = 'this line gets inserted'
html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + insert + r'\g<2>', html)

# html is now '<html> ... <defs>this line gets inserted<-- inserts will go here --> ... </html>'

If you make this into a function, you can keep injecting more lines repeatedly.

import re

def add_definitions(html, definitions):
  html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + ''.join(definitions) + r'\g<2>', html)
  return html

# then you can use it like this...
html = '<html> ... <defs><-- inserts will go here --> ... </html>'
html = add_definitions(html, [ 'add this line 1.', 'add this line 2.' ])

# html should now be '<html> ... <defs>add this line 1.add this line2.<-- inserts will go here --> ... </html>'

Upvotes: 0

Nelthar
Nelthar

Reputation: 55

For a string with multiple lines you should use triple single quotes or triple double quotes. And instead of "if" statement you better use a "for" loop.

Upvotes: 1

pferate
pferate

Reputation: 2107

You can concatenate strings with the + symbol.

>>> 'foo' + 'bar'
'foobar'

Upvotes: 0

Related Questions