简体   繁体   English

如何为 X.0.4472.114 之类的东西创建正则表达式,其中 X 是一个数字?

[英]How to creat a regex for a something like X.0.4472.114 where X is a number?

I want to create a regex for something from the template:我想为模板中的内容创建一个正则表达式:

X . X 。 number .数字 。 number number number number .号码号码号码号码。 number number号码 号码

or :要么 :

X . X 。 number .数字 。 number number number number .号码号码号码号码。 number number number号码号码号码

Where X is a variable that contains a number.其中 X 是一个包含数字的变量。

eg:例如:

X = 102 X = 102

then:然后:

102.3.2345.234 is a valid 102.3.2345.23 is valid too. 102.3.2345.234是有效的102.3.2345.23也是有效的。

I have tried to use "[" + X + "]" + "[.]0[.][\\\\d][\\\\d][\\\\d][\\\\d][.][\\\\d][\\\\d]" , but it's not working我曾尝试使用"[" + X + "]" + "[.]0[.][\\\\d][\\\\d][\\\\d][\\\\d][.][\\\\d][\\\\d]" ,但它不起作用

如果您在变量x有数字开头,那么您的正则表达式模式可以构造为

 String pattern = x + "\\.\\d\\.\\d{4}.\\d{2,3}";

You can use x + "\\\\.\\\\d\\\\.\\\\d{4}\\\\.\\\\d{2,3}" as the pattern.您可以使用x + "\\\\.\\\\d\\\\.\\\\d{4}\\\\.\\\\d{2,3}"作为模式。

Details of the regex:正则表达式的详细信息:

  • \\. : A dot : 一个点
  • \\d : A single digit \\d : 一位数
  • \\d{4} : 4 digits \\d{4} : 4 位数字
  • \\d{2,3} : 2 or 3 digits \\d{2,3} : 2 或 3 位数字

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

相关问题 如何确定看起来像这样的东西的大O:(x -1)+(x-2)+(x-3)…(x-x) - How to determine big O of something that looks like this: (x -1) + (x - 2) + (x - 3) … (x - x) 使用正则表达式匹配^ xb ^ x,其中x是a,b出现的次数 - Use regex to match a^xb^x, where x is the number of times a,b appear 显示数组的重复项,例如“数字x重复x次” - Display duplicates of an array like "Number x repeats x times 如何使在x:00 x:15 x:30和x:45上运行的线程在2:00做一些不同的事情 - How to make a thread that runs at x:00 x:15 x:30 and x:45 do something different at 2:00 如何用正则表达式匹配和排除“!x”? - How to match and exclude “!x” with regex? Java正则表达式:如何选择以特定字母开头并且x个字符长的单词? - Java regex: how to select words starting with a specific letter and is x number of characters long? 如何比较 (x,y) 点进行排序,其中 x 始终排在第一位 - How to compare (x,y) points for sorting, where x is always first 如何在java中实现(x pow y),其中x,y是double? - How to implement (x pow y) in java,where x,y are double? 找到非零整数x,其中x == -x? - Finding a nonzero integer x where x == -x? 这是如何运作的? x &lt;&lt; = 3 = -8其中(字节)x = 127? - How does this work? x<<=3 = -8 where (byte)x = 127?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM