Python Forum
converting a substring of digits and keep what follows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting a substring of digits and keep what follows
#1
i have a string with digits (possibly with a period) and something else at the end such as '1234.5foobar'. i want to extract and convert the number and get a string of what remains. if it were already a function, it would probably return a 2-tuple like (number,remaining_string). the use case goal is making a function that converts expressed time intervals in various fixed units such as '1wk1day3min5' -> 691385 or '2m7s' -> 127 or '1.1h' -> 3960. i know how to do this in single character steps as i have implemented this in C, before. i need the same thing in Python, now, and wonder if there is a shorter way using only what comes with Python (so i do not need to require the user of my code to install anything but Python, itself).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can try the following regex
import re

pat = re.compile(r'(\d+(?:\.\d*)?|\.\d+)')

data = """
1wk1day3min5
2m7s
1.1h
1.1.2
""".strip().split('\n')

for s in data:
    res = pat.split(s)
    print(res)
Output:
['', '1', 'wk', '1', 'day', '3', 'min', '5', ''] ['', '2', 'm', '7', 's'] ['', '1.1', 'h'] ['', '1.1', '', '.2', '']
In the resulting list, every odd index contains a number, every even index contains a possibly empty string between numbers.
Reply
#3
There is a great online regex tester https://regex101.com/#python courtesy of DeaD_EyE which even generates Python code.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
(Apr-21-2018, 02:10 PM)ljmetzger Wrote: There is a great online regex tester https://regex101.com/#python courtesy of DeaD_EyE which even generates Python code.

Lewis
copy and paste does not work on that page ... at least pasting the RE does not work. typing works, but i'm not very accurate at typing REs other people wrote. at least the forum post edit form lets me paste in ... (\d+(?:\.\d*)?|\.\d+)

it look like that regex should work for me. i can just pop off the first element from the list an loop through the rest popping them as i go, 2 at a time, a number and a multiplier suffix {'day':86400,...}.

maybe the next thing to ask for is a literal arithmetic expression evaluator (have done that in C, too). then somehow combine them so i can do things like '(1week3days)/5' and get 172800.

or maybe a general purpose units suffix thing.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 531 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,059 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 7,918 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,417 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Substring Counting shelbyahn 4 6,113 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Python Substring muzikman 4 2,307 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  Single digits seem to be greater than double digits Frosty_GM 4 3,493 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Removing items from list if containing a substring pythonnewbie138 2 2,180 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,351 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,588 Sep-23-2019, 05:34 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020