Home > front end >  Python regular expression for string with the following format
Python regular expression for string with the following format

Time:03-11

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:

'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.

CodePudding user response:

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 
  • Related