Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regexp. Cut string

I have strings like:

q.0.0.0.1-1111, q.0.0.0.1.tt_0-1111, tes-00000000-1111

I need cut this strings and get:

q.0.0.0-1111, q.0.0.0-1111, tes-1111

How i can get it with help of regexp?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If searching for specific one-pass regexp solution I’d suggest the following substitution:

import re

s = 'q.0.0.0.1-1111, q.0.0.0.1.tt_0-1111, tes-00000000-1111'
s = re.sub(r'(?<=[^-\s]{7})[^-]+(?=-)|(?<=-)[^-\s]+-', '', s)
print(s)

q.0.0.0-1111, q.0.0.0-1111, tes-1111
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading