简体   繁体   中英

java regex: match input starting with non-number or empty string followed by specific pattern

I'm using Java regular expressions to match and capture a string such as:

0::10000

A solution would be:

(0::\\d{1,8})

However, the match would succeed for the input

10::10000

as well, which is wrong. Therefore, I now have:

[^\\d](0::\\d{1,8})

which means it must lead with any character except a number, but that means there needs to be some character before the first zero. What I really want (and what I need help with) is to say "lead with a non-number or nothing at all."

In conclusion the final solution regular expression should match the following:

0::10000
kjkj0::10000

and should not match the following:

10::10000

This site may be of use if someone wants to help.

Thanks.

You need a negative lookbehind:

(?<!\d)(0::\d{1,8})

It means "match 0::\\d{1,8} not preceded by \\d ".

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