How to print number after end() method?

Question:

I filter a substring from a string.

Asked By: mightycode Newton

||

Answers:

text = "Koopliedenweg 38nDeb. nr. : 108636 2991 LN BARENDRECHTnYour VAT nr. : NL851703884B01 NederlandnFactuur datum : 19-11-21nAantal Omschrijving Prijs BedragnOrder number : 76372 Loading date : 15-11-21 Incoterm: : FOTnYour ref. : SCHOOLFRUIT Delivery date :nWK46nVerdi Import Schoolfruitn566 Ananas Crownless 14kg 10 Sweet CR Klasse I € 7,00 € 3.962,00n706 Appels Royal Gala 13kg 60/65 Generica PL Klasse I € 4,68 € 3.304,08n598 Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I € 6,30 3.767,40nOrder number : 76462 Loading date : 18-11-21 Incoterm: : FOTnYour ref. : SCHOOLFRUIT Delivery date"

appels_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'

def make_pattern(substr):
    return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)n"

allSubstring = re.findall(make_pattern(appels_royal_gala), text)
print(allSubstring[0])

# Prints
3.304,08

If you care about the index, you can still use re.search and then you should do print(allSubstring[1]) (instead of 0).

This solution assumes the number you’re looking for is allways followed by n, which seems to be a constant in your example.

Answered By: Ignatius Reilly
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.