简体   繁体   中英

Regex - allow some characters only if the string contains any other characters

I've been messing around with regex for a few days now and I've done everything but one small thing.

^[-,.'\s\p{L}]{1,}$

For example, the regex above should allow any name (including whitespaces, commas, etc.), but right now it also counts " " and ",.-' " as names. The question is simple: how can I prevent that? Sorry if that's a duplicate question, I couldn't find an exact same problem.

The simple answer is to allow any number of your allowed characters before, and after, a real character.

Ie ^[-,.'\\s\\p{L}]*\\p{L}[-,.'\\s\\p{L}]*$

Regards

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