简体   繁体   中英

Java Regular Expressions Special characters and the & sign

I have a regular expression that searches for all uppercase words but I need a regular expression that will search for special characters and the "&" Character. For example I would need a regular expression that would find words like "A&E" or "A/V". Below is my standard Regular Expression for all uppercase words.

String twoPlusUCRegEx = "[A-Z][A-Z]+\\s";

Just include the special characters you want in the second [AZ] category. For example:

"[A-Z][A-Z&/]+\\s"

If you want all US-ASCII punctuation, you can use a POSIX character class:

"[A-Z][A-Z\p{Punct}]+\\s"

You might also want to put a word boundary or the like at the start of your re, so you don't match strings like "aAB".

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