简体   繁体   中英

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 "." 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 "." (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.

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

Regex demo

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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