Skip to content
Advertisement

Using Split method or Regex to separate string

In my project I am webscrapping UFC website to gather to the total wins, total losses, and total draws of each UFC athlete.

This is part of my code, as I wish to strip the total wins, total losses, and total draws separately:

import re
record = "10-7-3 (W-L-D)" #W = wins, L= Loss, D= Draws
char = "-"
record+="-"
totalwins = ""
totalloss = ""
totaldraws = ""


correctRecord = re.findall('[[^]]*]|([^)]*)|"[^"]*"|S+',record)[0]

print("The correct record per fighter is:", correctRecord)

totaldash = 0
for i in range(len(correctRecord)):
    if(record[i] == char):
    
        totaldash+=1
   
   
        if totaldash == 1:
            print("The total wins", totalwins)
            totalwins =""
        elif totaldash ==2:
       
            print("The total losses ", totalwins)
       
            totalwins=""
       
        elif totaldash ==3:
       
            print("The total draws ", totalwins)
   
   
    elif (correctRecord[i] !=char):
        totalwins +=correctRecord[i]

The result is the following:

   
   The correct record per fighter is: 10-7-3
   The total wins 10
   The total losses  7

The problem is, I am unable to spew out the total draws. I also tried using the strip method with no avail:

correctRecord= str(record.split(separator, 1)[0])

Advertisement

Answer

Try:

import re

record = "10-7-3 (W-L-D)"

wins, loss, draw = map(int, re.findall(r"d+", record))

print(f"{wins=} {loss=} {draw=}")

Prints:

wins=10 loss=7 draw=3
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement