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

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.#&,’()+/:]*$

Related

Regex function wont match. How to write correctly

Trying to create Regex code that starts with # and then matches any alphanumeric character.
How do I make a code for Regex to match a line that starts with # and then followed by \w ?
I believe you want the following:
^#\w+
The ^ makes sure the string actually starts with the expression that follows.
We only check for one # character so we just place that there without anything else.
The \w includes all alphanumeric characters and underscores.
The + ensures there is at least one of the preceding token (at least one alphanumeric character in your case).
You should really try to attempt to learn expressions on your own though and try before you ask. I recommend this website to learn from: https://regexr.com/

(MVC) Regex: starts with, ends with and can only contain

I'm writing a data annotation in an MVC application. I need to apply a RegEx for the following:
Must start with an alphabetic char,
Must end with an alphabetic char,
Can only contain alphabetic, periods/full stops, spaces, apostrophes
and hyphens.
I'm trying the following and would appreciate a point in the right direction:
^[A-Za-z][A-Za-z|.| |'|-]*(?:[A-Za-z])*$
Effectively it's appearing to do what I want, EXCEPT that it allows periods, hyphens, spaces and apostrophes at the end of the string. I thought I had cracked it, but instead, I'm turning to you SO.
Any help would be very much appreciated!!
EDIT:
Just in case anyone is after the solution to a very similar problem:
^[A-Za-z][A-Za-z|.| |'|-]*[A-Za-z]$
Just remove the lookahead at the very end of your regex and you should be done.
/^[A-Za-z][A-Za-z|.| |'|-]*[A-Za-z]$/g
What you want to make sure, is that the very last letter of your match is a letter, so there is no need to have a lookahead. You can just match it like you would do with any normal letter.

Regex for allowing particular Special Characters

I need a regex for allowing list of special characters((_-.$#?,:'/!) and letters supporting utf-8 languages.
I tried
/^[\_\-\.\$#\?\,\:\'\/\!]*$/
but typing letters in English and Tamil shows invalid.
You need to escape the hyphen for it to be valid. You also don't need to escape most of the other characters - inside of brackets, almost everything is literal.
/[_\-.$#?,:'/!]*/
I have no idea if your regex engine supports \p{L}. You can try this:
^[_\-.\$#\?\,\:\'/!\p{L}]*$
or this one:
^[_\-.\$#\?\,\:\'/!\w]*$
The last one also matches digits.

In Regular Expression, disable double dashes

I have this RegExp:
^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)([a-zA-Z]|)(\:|)[^\x00-\x1f\'\?\-\*\:\"\;\|\/]+$
This do not allow filenames with a single dash. But I would like to do not allow only double dashes (anywhere in the filename/folder), single dash should be ok.
Thanks for any info.
Change the [^\x00-\x1f\'\?\-\*\:\"\;\|\/]+ at the end into an expression which allows this character class, optionally followed by a dash, followed by this character class, repeated any number of times. Add an optional leading and trailing dash as well if you like. (I have added them here because it's easier than to explain :-)
^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)([a-zA-Z]|)(\:|)-?[^\x00-\x1f\'\?\-\*\:\"\;\|\/]+(-[^\x00-\x1f\'\?\-\*\:\"\;\|\/]+)*-?$
I have required at least one non-dash character; if you want to allow a single dash, the first non-optional group could include that instead, but then the trailing context will have to look different.
I would use non-capturing groups but you're not telling which regex flavor you are using, so maybe you don't have them.

Password validation regex

I am trying to get one regular expression that does the following:
makes sure there are no white-space characters
minimum length of 8
makes sure there is at least:
one non-alpha character
one upper case character
one lower case character
I found this regular expression:
((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!\s).{8,})
which takes care of points 2 and 3 above, but how do I add the first requirement to the above regex expression?
I know I can do two expressions the one above and then
\s
but I'd like to have it all in one, I tried doing something like ?!\s but I couldn't get it to work. Any ideas?
^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\S{8,}$
should do. Be aware, though, that you're only validating ASCII letters. Is Ä not a letter for your requirements?
\S means "any character except whitespace", so by using this instead of the dot, and by anchoring the regex at the start and end of the string, we make sure that the string doesn't contain any whitespace.
I also removed the unnecessary parentheses around the entire expression.
Tim's answer works well, and is a good reminder that there are many ways to solve the same problem with regexes, but you were on the right track to finding a solution yourself. If you had changed (?!\s) to (?!.*\s) and added the ^ and $ anchors to the end, it would work.
^((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,})$