Garvey
Garvey

Reputation: 1309

Extract number from str with pattern

Example like "3-5/description". I'd like extract the numbers next to the dash -, which are 3 and 5 in this example. The description next to the / is a str containing no number.

I want a tool to help me extract number from str like this like this func("3-5/description")returns [3,5]

Upvotes: 0

Views: 46

Answers (1)

KBN
KBN

Reputation: 153

You can do a regex match to capture the digits before and after the '-'

import re

def func(input):
    return re.match(r'(\d+)[-](\d+)', x).groups()

func("3-5/description")

Upvotes: 1

Related Questions