Home > Software design >  Split part of string after character, always same amount
Split part of string after character, always same amount

Time:12-27

I am looking to get part of a string after an = sign that will always be the same amount of characters, after the =.

Ex.

str = [<User id=000000000000000000 name='test' discriminator='0001'] - 

I want to be able to get the 000000000000000000 and nothing else. The total length of the string may change, but the numbers I am looking to collect are always going to be 18 numbers, after the id=.

CodePudding user response:

If you want 18 digits, after the id=:

import re

regex = r"id=(\d{18})"
test_str = "str = [<User id=000000000000000000 name='test' discriminator='0001'] - \n"

print(re.findall(regex, test_str))

Result:

['000000000000000000']

CodePudding user response:

You could use re.findall here:

inp = "str = [<User id=000000000000000000 name='test' discriminator='0001']"
id = re.findall(r'\bid=(\d )\b', inp)[0]
print(id)  # 000000000000000000
  • Related