1

I can't find a solution to this, so I'm asking here. I have a string that consists of several lines and in the string I want to increase exactly one number by one. For example:

[CENTER]
[FONT=Courier New][COLOR=#00ffff][B][U][SIZE=4]{title}[/SIZE][/U][/B][/COLOR][/FONT]

[IMG]{cover}[/IMG]


[IMG]IMAGE[/IMG][/CENTER]
[QUOTE]
{description_de}
[/QUOTE]

[CENTER]



[IMG]IMAGE[/IMG]

[B]Duration: [/B]~5 min
[B]Genre: [/B]Action
[B]Subgenre: [/B]Mystery, Scifi
[B]Language: [/B]English
[B]Subtitles: [/B]German
[B]Episodes: [/B]01/5


[IMG]IMAGE[/IMG]
[spoiler]
[spoiler=720p]
[CODE=rich][color=Turquoise]
{mediaInfo1}
[/color][/code]
[/spoiler]
[spoiler=1080p]
[CODE=rich][color=Turquoise]
{mediaInfo2}
[/color][/code]
[/spoiler]
[/spoiler]



[hide]
[IMG]IMAGE[/IMG]
[/hide]
[/CENTER]

I'm getting this string from a request and I want to increment the episode by 1. So from 01/5 to 02/5.

What is the best way to make this possible?

I tried to solve this via regex but failed miserably.

kiddo
  • 13
  • 4
  • What would be the criterium to decide the number should be 01 after "Episodes" and not the number 5 after "Duration"? Is it always the number after Duration? It depends on user input? Is the number always with the format xx/y? – Ignatius Reilly Nov 15 '22 at 18:27
  • 1
    Does the number always follow the pattern `x/y`, and is there guaranteed to be only one occurrence of that pattern? – John Gordon Nov 15 '22 at 18:29
  • You can use `re.sub()`. The replacement can be a function, so it can add 1 to the number that was matched. – Barmar Nov 15 '22 at 18:31
  • @IgnatiusReilly This string is a post from a thread on a forum. I would like to use a script to always increase the number of the current episode by 1 when I run it. The content of the post should remain exactly the same, except for the number of episodes, which increases by 1. So, whenever I run the script, I want it to increment that exact number by 1. In the end I want to have the same string again, just with the increased number so I can update the original post. – kiddo Nov 15 '22 at 18:34
  • @JohnGordon Exactly, it always stays in the same pattern. – kiddo Nov 15 '22 at 18:35
  • Thing is, the string is way longer than what I specified. I only showed the part that is relevant. – kiddo Nov 15 '22 at 18:41

2 Answers2

0

Assuming the number you want to change is always after a given pattern, e.g. "Episodes: [/B]", you can use this code:

def increment_episode_num(request_string, episode_pattern="Episodes: [/B]"):
    idx = req_str.find(episode_pattern) + len(episode_pattern)
    episode_count = int(request_string[idx:idx+2])
    return request_string[:idx]+f"{(episode_count+1):0>2}"+request_string[idx+2:]

For example, given your string:


req_str = """[B]Duration: [/B]~5 min
            [B]Genre: [/B]Action
            [B]Subgenre: [/B]Mystery, Scifi
            [B]Language: [/B]English
            [B]Subtitles: [/B]German
            [B]Episodes: [/B]01/5
            """

res = increment_episode_num(req_str)
print(res)

which gives you the desired output:

[B]Duration: [/B]~5 min
[B]Genre: [/B]Action
[B]Subgenre: [/B]Mystery, Scifi
[B]Language: [/B]English
[B]Subtitles: [/B]German
[B]Episodes: [/B]02/5
Luca Clissa
  • 810
  • 2
  • 7
  • 27
0

As @Barmar suggested in Comments, and following the example from the documentation of re, also formatting to have the right amount of zeroes as padding:

pattern = r"(?<=Episodes: \[/B\])[\d]+?(?=/\d)"

def add_one(matchobj):
    number = str(int(matchobj.group(0)) + 1)
    return "{0:0>2}".format(number)

re.sub(pattern, add_one, request)

The pattern uses look-ahead and look-behind to capture only the number that corresponds to Episodes, and should work whether it's in the format 01/5 or 1/5, but always returns in the format 01/5. Of course, you can expand the function so it recognizes the format, or even so it can add different numbers instead of only 1.

Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15