Restless
Restless

Reputation: 21

How to get first number from a string with example

I have text that looks like this:

Owen White punt for 26 yards, downed at the army 37

I want to get the punt length from this expression, in this case 26 yards. How could i do this?

To answer some questions: I have about 800 of these sentences, and I want to fill a list with the amount of yards( in this case, 26) So I cannot add both values to a list, only the first.

Upvotes: 1

Views: 1300

Answers (6)

Petr Fořt Fru-Fru
Petr Fořt Fru-Fru

Reputation: 996

I have solved a similar problem in the past. If you know that the number you are looking for is in a specific position in the string (you know that it will be the first or second). Try it (without regex) like this:

a_string = "Owen White punt for 26 yards, downed at the army 37"
result = []
for word in a_string.split():
    if word.isdigit():
        result.append(int(word))

print(result[0]) #Output: 26

Upvotes: 0

Databash
Databash

Reputation: 86

If you want the string '26 yards' as a result, and the format of your input string is always '[digit] yards' then you could use this piece of code which uses regular expression and prints a list with every match of '[digit] yards' from my_string:

import re
my_string = 'Owen White punt for 26 yards, downed at the army 37'
regex_pattern = r'\d+ yards'
result = re.findall(regex_pattern, my_string)
print(result)

The first r in the regex pattern specifies that it is a raw string, it means it is stored as it appears. For example, '' is just a backslash instead of an escaping. Then the regex pattern starts: \d means it looks for digits, * means 1 or more of the preceding token, so in this case one or more digits. Then yards, which is just the string as it is.

Upvotes: 0

liminal_
liminal_

Reputation: 91

Try using regex. There are plenty of tools that can help you create match patterns for capturing specific characters in strings as well as rules for what to capture.

Upvotes: 0

Pierre D
Pierre D

Reputation: 26211

If you want to get 26 from 'foo123 has 26 bars', then:

val = [int(v) for v in s.split() if v.isnumeric()][0]

But if you want to get 123 instead, then:

val = int(re.search(r'\d+', s).group())

Upvotes: 1

Mainly Karma
Mainly Karma

Reputation: 477

You could do something like this

sentence = 'Owen White punt for 26 yards, downed at the army 37'
first_digit_in_sentence = ''
words = sentence.split(' ')

for word in words:
    if word.isdigit():
        first_digit_in_sentence = word
        break;

print(first_digit_in_sentence)

Upvotes: 0

berkayln
berkayln

Reputation: 995

You can use regex as below:

import re
str = "Owen White punt for 26 yards, downed at the army 37"
#search using regex
x = re.findall('[0-9]+', str)
print(x[0])

Upvotes: 2

Related Questions