简体   繁体   English

Java 正则表达式匹配; 第一个小数点前的下划线

[英]Java RegEx Match; underscore before first decimal point

I am trying to find a Java regex that will match an "_" before the instance of the first decimal point "."我试图找到一个 Java 正则表达式,它会在第一个小数点“.”的实例之前匹配一个“_”。 Below doesn't seem to be working for me:以下似乎对我不起作用:

_(?=[0-9][0-9]\.)

Anyone have an idea on how I can accomplish this?任何人都知道我如何做到这一点? Example would be:示例是:

FileName:文档名称:

Hello_World_5_01.00.0000

I would like to match the "_" between the characters "5_01."....Keep in mind that the above is an example filename and will vary, but the one constant will be the "_" before the instance of the first "."我想匹配字符“5_01.”之间的“_”......请记住,上面是一个示例文件名并且会有所不同,但一个常量将是第一个实例之前的“_” “。” (decimal point) regardless of the filename itself. (小数点)与文件名本身无关。 Any help is appreciated.任何帮助表示赞赏。

You could match until the first occurrence of an underscore and capture that in a group.您可以匹配直到第一次出现下划线并在组中捕获它。

Then match the following 1+ digits, a dot and if there has to be a digit following you can match that as well.然后匹配以下 1+ 位数字,一个点,如果后面必须有一个数字,您也可以匹配它。

^.*?(_)\d+\.\d

Regex demo正则表达式演示

 const regex = /^.*?(_)\\d+\\.\\d/; const str = `Hello_World_5_01.00.0000`; console.log(str.match(regex)[1]);

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

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