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

Python regular expression for string with the following format

I’m having trouble making a Regex to find a string matching the format of:

'One or more numeric digits///Any combination of alphanumerics and non-alphanumerics///Any combination of alphanumerics and non-alphanumerics///Any combination of alphanumerics and non-alphanumerics'

A more specific example would be:

'Number of transactions///Total Revenue///Product name///Cost of Supplies'

Which would look something like:

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

'1002///1502.34///Coca-Cola-12.Oz///902.23'

There are no whitespaces in the strings.

I’ve tried the Regex: r'\d+///\d+.\d+///\w+///\d+.\d+'
The problem is that for the \w+ section because it can sometimes contain non-alphanumeric characters.

>Solution :

You can use a character class in between the words to allow what characters should be matched. In this case you could add a . and a -

Note to escape the dot to match it literally.

\d+///\d+\.\d+///\w+(?:[.-]\w+)*///\d+\.\d+

Regex demo

Other options could be using only the character class or if you don’t want to allow / in between, a negated character class:

\d+///\d+\.\d+///[\w.-]+///\d+\.\d+
\d+///\d+\.\d+///[^/\n]///\d+\.\d+
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