简体   繁体   中英

Regex for Alphanumeric characters, .@&,’()+/: and one hyphen only

I have a regex for matching letters, numbers and some special characters as follows: ^[A-za-z0-9 .@&,'()+/:]*$

I need to add a single hyphen to this list, not allowing multiple hyphens, but I'm not quite sure how to do it. I saw something along the lines of -{1} but I don't know how to add that to the existing rexex.

I'm using C++ and Qt5.

How about:

^[A-za-z0-9 .@&,’()+/:]*-?[A-za-z0-9 .@&,’()+/:]*$

that could be reduce to:

^[\w .@&,’()+/:]*-?[\w .@&,’()+/:]*$

I don't know if C++ support it, but it could be reduced to:

^([\w .@&,’()+/:])*-?(?1)*$

^[A-za-z0-9.@&,'()+/:]*-[A-za-z0-9.@&,'()+/:]*$ allows a single hyphen anywhere in the string.

Note that the hyphen may come at any part (at the beginning or end of the string also) and it is mandatory also.

To make the hyphen optional, use ^[A-za-z0-9.@&,'()+/:]*-?[A-za-z0-9.@&,'()+/:]*$

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