Python Forum
trying to recall a regex for re.split()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to recall a regex for re.split()
#17
Look at right side on regex101 have explanation.
Think i have link to Regex101 a couple time before when you have about Regex
Quote:on what basis should i make the choice between re.split() vs. re.search()?
Many of regex method work in a similar way,but have specific use case they work better.
as eg search,findall,match can all be used to split and and solve this case.
re.spilt is more specialized and as name say more about splitting and it handle groups different.
Gribouillis regex pattern also work with re.spilt if add a group to it ()
import re

def split_suffix(arg):
    return re.split(r"([+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)", arg)[1:]

if __name__ == '__main__':
    lst = [
        "144mHz",
        "1.296GHz",
        "100m/h",
        "1.25E-20watts",
        "1.23e+07GHz",
        "3.45e-4mHz",
        "12345abc",
    ]
    for n in lst:
        print(f'{n:<13} {split_suffix(n)}')
Output:
144mHz ['144', 'mHz'] 1.296GHz ['1.296', 'GHz'] 100m/h ['100', 'm/h'] 1.25E-20watts ['1.25E-20', 'watts'] 1.23e+07GHz ['1.23e+07', 'GHz'] 3.45e-4mHz ['3.45e-4', 'mHz'] 12345abc ['12345', 'abc']
Reply


Messages In This Thread
RE: trying to recall a regex for re.split() - by snippsat - May-19-2022, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split pdf in pypdf based upon file regex standenman 1 2,307 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  recall cool_person 1 1,106 May-07-2022, 08:04 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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