简体   繁体   English

如何在Java中使用正则表达式检查数字?

[英]How to check numeric using Regular Expressions in Java?

I have given file name as 我给的文件名为

B-018 MyName

I want to check whether first four characters are in format of 我想检查前四个字符是否采用以下格式:

'B-< Numeric >< Numeric >< Numeric > 'B- <数字> <数字> <数字>

B- will be constant for all names and i want to check if three succeeding characters are numeric. B-对于所有名称都是恒定的,我想检查三个连续字符是否为数字。

How to do it using Regular Expressions in Java? 如何使用Java中的正则表达式做到这一点?

The following expression implements your requirement. 以下表达式实现您的要求。

java.util.regex.Pattern.compile("B-\\d{3} .*").matcher("B-018 MyName").matches()

It checks that the expressions is "B-(3-digits) (any character zero or more time)". 它检查表达式是否为“ B-(3位)(零个或多个时间的任何字符)”。

I advise you to use a site as https://regex101.com/ to learn some basis about regex and test your expressions. 我建议您使用https://regex101.com/网站,以了解有关正则表达式的一些基础并测试您的表情。 This expression can be retrieved using this link https://regex101.com/r/B0GN8F/1 . 可以使用此链接https://regex101.com/r/B0GN8F/1检索此表达式。

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

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