Matching number string pairs

2024/10/5 22:27:55

I have the following sample string:

 R10666: 273141 C1 + 273141 C2 + 273141 C3 + 273141 C4 + 273141 C5 - 273141 C6

I want to obtain:

[('273141','C1'), ..., ('- 273141', 'C6')]

The numbers can be floating point numbers with exponential notation i.e. - 2.5e-7.

My current regex looks like this:

re.findall(r'([+-]? \d+(\.\d*)?|\.\d+([eE][+-]?\d+)?)( [a-zA-Z0-9_]+)', split)

But it doesn't produce the correct output, what is wrong with it?

This is a sample output:

(' 273141', '', '', ' C1')

or it matches nothing.

Answer

findall will put all the submatches in the results. In your case, the empty strings come from the unmatched decimals if they are present; so use non-capture groups instead:

([+-]? \d+(?:\.\d*)?|\.\d+(?:[eE][+-]?\d+)?) ([a-zA-Z0-9_]+)

I also moved the space at the second capture group outside so you don't get that leading space.

regex101 demo

ideone demo

https://en.xdnf.cn/q/119019.html

Related Q&A

Turning a text file into a tabular format [duplicate]

This question already has answers here:How do I print parameters of multiple objects in table form? [duplicate](2 answers)Line up columns of numbers (print output in table format)(7 answers)Closed 5 y…

Python: Read file with list as list

I have placed a list in a text file. I want python to read the text file and return the contents as a list. However, it is instead reading the content as a string:Text file:[a,b,c]Python:ids=[]writtenF…

Tkinter scrollbar not scrollable

I followed some tutorial on attaching a scrollbar to a textbox. However, in the tutorial, the scrollbar is really a "bar". When I tried myself, I can only press the arrows to move up or down,…

How to create multiple roles through discord.py bot?

I have been trying to make my discord bot create multiple roles through a command. But it simply doesnt work. Here is what I have done so far: @commands.command()async def create_roles(self, ctx):guild…

python: how do i know when i am on the last for cycle

for i in range(len(results_histogram)):if i!=len(results_histogram)-1:url+=str(results_histogram[i])+,my if statement is checking whether i am on the last loop, but it is not working. what am i doing w…

scrape text in python from https://brainly.co.id/tugas/148

scrape "Jawaban terverifikasi ahli" in green box from the url https://brainly.co.id/tugas/148, possibly the color of green tick icon to the left of it also(tag <use xlink:href="#icon-…

Percentage of how similar strings are in Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

A Python dictionary with repeated fields

Im constructing a dictionary with Python to use with a SOAP API.My SOAP API takes an input like this:<dataArray><AccountingYearData><Handle><Year>string</Year></Handle&…

psexec run python script passed from host

I am trying to run a python script on a remote computer via psexec. I am able to connect and run python.exe with the following:C:\test>psexec \\192.168.X.X -u domain\administrator -p password -i C:…

TypeError: main() missing 1 required positional argument: self

My code and error is below and I was trying to understand why I am getting the error and how to fix it. I tried this without self and got another error TypeError: load_data() takes 0 positional argumen…