简体   繁体   English

Java字符串的正则表达式包含包含字母和:/

[英]Regular Expression for a Java String Contains contains Alphabet and : /

I have a set of Strings like "04/21 01:55 P " , "1" , "10/21" . 我有一组字符串,例如"04/21 01:55 P ""1""10/21" I wrote a regex as follows 我写了一个正则表达式如下

^([[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2} P|A ]+)

It should accept only the format of Strings like "04/21 01:55 P " . 它应仅接受字符串格式,例如"04/21 01:55 P " But it is also accepting strings like "1" , "10/21" 但是它也接受诸如"1""10/21"类的字符串

Could any one let me know where I want wrong. 谁能让我知道我想错了什么。

Replace the surrounding [] by () . 将周围的[]替换为()

You'll need to change the P|A part too, either by (P|A) or [PA] . 您还需要通过(P|A)[PA]来更改P|A部分。

You've put everything in one big character class, which is why single digits are being matched as well. 您已将所有内容都放在一个大字符类别中,这就是为什么还要匹配单个数字的原因。 You can try something like 您可以尝试类似

^(\d{2}/\d{2} \d{2}:\d{2} (?:P|A) )+

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

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