Regex behaving weird when finding floating point strings [duplicate]

2024/10/5 14:57:03

So doing this (in python 3.7.3):

>>> from re import findall
>>> s = '7.95 + 10 pieces'
>>> findall(r'(\d*\.)?\d+', s)
['7.', '']   # Expected return: ['7.95', '10']

I'm not sure why it doesn't find all the floats inside? Is this possibly some python quirk about capturing groups?

My logic behind the regex: (\d*\.)? matches either 1 or none of any number of digits, followed by a period. \d+ then maches any number of digits, so this regex should match any of 11.11, 11, .11 and so on. Whats wrong here?

Answer

As you guessed correctly, this has to do with capturing groups. According to the documentation for re.findall:

If one or more groups are present in the pattern, return a list of groups

Therefore, you need to make all your groups () non-capturing using the (?:) specifier. If there are no captured groups, it will return the entire match:

>>> pattern = r'(?:\d*\.)?\d+'>>> findall(pattern, s)
['7.95', '10']
https://en.xdnf.cn/q/119839.html

Related Q&A

how do i get this code to tell me what position a word in the sentence is

varSentence = ("The fat cat sat on the mat")print (varSentence)varWord = input("Enter word ")varSplit = varSentence.split()if varWord in varSplit:print ("Found word") else…

Reverse geocoding with Python/GoogleMaps API: How to parse response

Im attempting to use the Google Maps Geocoding API to find the State associated with a latitude and longitude with a Python script. I have no trouble getting back a response from the server, but when …

How to constantly generate random numbers inside for i in range loop

import random world_size=8 for i in range(world_size)chunk_type=random.randint(1,2)print(chunk_type)import randomclass chunk_type():chunk_type=random.randint(1,2)world_size=8for i in range(world_size):…

How to count IDs that have a given combination of flags?

I have dataframe like that. I need to choose and count all distinct users, who have title "banner_click" and "order". So I dont understand how to do it in pandas, in SQL you do like…

How to calculate quarterly wise churn and retention rate using python

How to calculate quarterly wise churn and retention rate with date column using python. with date column i want to group that quarterly using python.This is used to calculate the churn count groupby qu…

splitting a list into two lists based on a unique value

I have a text file that looks something like this:hello 12 hello 56 world 25 world 26Is there a way in python that I can somehow parse the list that I obtain from reading this data to obtain two separa…

Customize axes in Matplotlib

I am a beginner with Python, Pandas and Matplotlib. I would like to customize the entries at the axes of a scatter plot. I have the following data:So on the x-axis there should be 5 entries, with the f…

how to show the max and min from user input?

Nevermindif xmin == 1:print(ymin)I tried using the max and min but I get a typeerror which is int object not iterable.

To output a string without whitespce

My program:def string_splosion(str):j=1c=len(str)i=0s=while(i<c):s=s+(str[:i+j])i=i+1print sprint("Enter a string:") s=raw_input() string_splosion(s)Sample input:Code Expected output:CCoCo…

Regex replace `a.b` to `a. b`? [duplicate]

This question already has answers here:Python: Replace with regex(2 answers)Closed 6 years ago.I am trying to change all strings of the type a.b to a. b. It should also work if any of the characters ar…