用java编写正则表达式
我试图编写正则表达式来匹配特定的模式用java编写正则表达式,java,regex,Java,Regex,我试图编写正则表达式来匹配特定的模式 // 1. 1:15 // 2. 3:15 PM // 3. (3:15) PM // 4. (3:15 PM) // 5. DIGITAL PROJECTION 1:35 AM // 6. (1:15) // 7. DIGITAL PROJECTION (1:35 AM) // 8. RWC/DVS IN DIGITAL PROJECTION (11:40 AM) 我能写的是 (.*)??\\s?\\(?(\\d{1,2})[:](\\d{1,2})\\
// 1. 1:15
// 2. 3:15 PM
// 3. (3:15) PM
// 4. (3:15 PM)
// 5. DIGITAL PROJECTION 1:35 AM
// 6. (1:15)
// 7. DIGITAL PROJECTION (1:35 AM)
// 8. RWC/DVS IN DIGITAL PROJECTION (11:40 AM)
我能写的是
(.*)??\\s?\\(?(\\d{1,2})[:](\\d{1,2})\\)?\\s?(\\w{2})?
它适用于前5个示例,但不适用于其他,我在这个正则表达式中看到的2个问题是,例如6,我希望组1为空,而示例8将组1返回为“RWC/DVS数字投影”(“但我只想要“RWC/DVS数字投影”您是否在寻找类似的东西:
^(.*?)\W*(\d{1,2}):(\d{1,2})\W*([AaPp][Mm])?.*$
这里有一个解释
^ <-- Beginning of the line
(.*?) <-- Match anything (but ungreedy)
\W* <-- Match everything that's not a word/number (we'll ignore that)
(\d{1,2}) <-- Match one or two digits (hours)
: <-- :
(\d{1,2}) <-- Match one or two digits (minutes) [You should consider only matching two digits]
\W* <-- Match everything that's not a word/number (we'll ignore that)
([AaPp][Mm])? <-- Match AM or PM (and variants) if it exists
.* <-- Match everything else (we'll ignore that)
$ <-- End of the line
^您能解释一下格式是什么吗?请通过解释您的正则表达式应该匹配的内容(以及不应该匹配的内容)来澄清您的问题。很抱歉没有说得更清楚,但@Colin回答了我的问题