How to deal with Java regular expressions?

pietà :

I'm dealing with regular expressions, but I’m not a big fan of it and I’m obliged to deal with it in my task :(

I have passed hours looking for a solution but everytime I fail to cover all scenarios. I have to write a regular expression template that supports these patterns:

 DYYU-tx-6.7.9.7_6.1.1.0 
 DYYU-tx-6.7.9.7_60.11.11.09 
 DYYU-tx-60.70.90.70_6.1.1.0 

I feel that this is very simple to do.. So excuse me if it's a stupid question for someone :(

I tried this pattern but it didn’t work : ^.*_.*-.*-([0-9]*)\\..*\\..* $

Any help please. I will be more than thankful.

mrzasa :

Try this one:

^\w+-\w+-(\d+)(\.\d+)+_(\d+\.)+\d+

Demo

In Java most probably sth like this:

"^\\w+-\\w+-(\\d+)(\\.\\d+)+_(\\d+\\.)+\d+"

Explanation:

  • ^\w+-\w+- first two parts, e.g. DYYU-tx-
  • (\d+)(\.\d+)+_ numbers separated with . ending with _, e.g. 6.7.9.7_
  • (\d+\.)+\d+ numbers separted with ., e.g. 60.11.11.09

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127081&siteId=1