简体   繁体   English

正则表达式,用于匹配由“ $”字符分隔的正非零双精度字符串

[英]Regex for matching a string of positive non-zero doubles seperated by the “$” character

Context : I have found a solution for matching three Integers seperated by the "$" character as seen below: 上下文 :我找到了一种匹配三个以“ $”字符分隔的整数的解决方案,如下所示:

String toMatch = "123$53$12"; //Returns true
String toMatch2 = "123$0$12"; //Returns false
String toMatch3 = "0$53$12";  //Returns false
String toMatch4 = "123$53$0"; //Returns false
System.out.println(toMatch.matches("\\d+.*\\d+.*\\d+") && !toMatch.matches([^0-9]*0[^0-9]*"));

Problem : What I want to achieve is: 问题 :我想要实现的是:

String toMatch = "123.43$.03$123.0"; //Returns true
String toMatch2 = "123$000000$12";   //Returns false
String toMatch3 = "0.0000$53$12";    //Returns false
String toMatch4 = "123$53$.000";     //Returns false

Essentially what I want is a Regex matching 3 numbers separated by the "$" character, with each number being a positive non-zero double if parsed by the Double.parseDouble() method. 本质上,我想要的是一个正则表达式,它匹配由“ $”字符分隔的3个数字,如果通过Double.parseDouble()方法进行解析,则每个数字都是正的非零双Double.parseDouble()

If I've correctly understood, I think this will work: 如果我正确理解,我认为这会起作用:

^(?!\\$)((^|\\$)(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)){3}$

Follows an explenation: 如下图所示:

  • ^(?!\\\\$) : the begin of the match must not be followed by a '$' ^(?!\\\\$) :比赛开始后不得带'$'
  • {3} : the following pattern has to be repeated 3 times {3} :以下模式必须重复3次
    • (^|\\\\$) : the pattern starts or with the begin of the string or with a '$' (not both, for what stated above) (^|\\\\$) :模式以字符串的开头或字符串的开头或以“ $”开头(对于上述内容,不能同时为两者)
    • (?=[^$]*[1-9]) : before the next eventual '$' there must be a non-0 digit (?=[^$]*[1-9]) :下一个最终的“ $”之前必须有一个非0的数字
    • (\\\\d+(\\\\.\\\\d*)?|(\\\\.\\\\d*)?\\\\d+) : the allowed format for the number is either \\d+(\\.\\d*)? (\\\\d+(\\\\.\\\\d*)?|(\\\\.\\\\d*)?\\\\d+) :数字的允许格式为\\d+(\\.\\d*)? or (\\.\\d*)?\\d+ (\\.\\d*)?\\d+
  • $ : end $ :结束

See here for a demo 观看这里的演示

An extended expression (if you don't trust the repeat trick) is: 扩展表达式(如果您不相信重复技巧)是:

^(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)\\$(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)\\$(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM