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

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.

Related

PowerShell RegEx with multiple options

Given a file name of 22-PLUMB-CLR-RECTANGULAR.0001.rfa I need a RegEx to match it. Basically it's any possible characters, then . and 4 digits and one of four possible file extensions.
I tried ^.?\.\d{4}\.(rvt|rfa|rte|rft)$ , which I would have thought would be correct, but I guess my RegEx understanding has not progressed as much as I thought/hoped. Now, .?\.\d{4}\.(rvt|rfa|rte|rft)$ does work and the ONLY difference is that I am not specifying the start of the string with ^. In a different situation where the file name is always in the form journal.####.txt I used ^journal\.\d{4}\.txt$ and it matched journal.0001.txt like a champ. Obviously when I am specifying a specific string, not any characters with .? is the difference, but I don't understand the WHY.
That never matches the mentioned string since ^.? means match beginning of input string then one optional single character. Then it looks for a sequence of dots and digits and nothing's there. Because we didn't yet pass the first character.
Why does it work without ^? Because without ^ it is allowed to go through all characters to find a match and it stops right before R and continues matching up to the end.
That's fine but with first approach it should be ^.*. Kleene star matches every thing greedily then backtracks but ? is the operator here which makes preceding pattern optional. That means one character, not many characters.

Improving the below RegEx for US and UK Names

I'm looking to validate English names. How could I improve this RegEx in order to ensure that the hyphen, comma or apostrophe does not come first or last in the string? Further, a hyphen or apostrophe must always be directly preceded and succeeded with a letter.
I have the RegEx: [a-zA-Z-', ].
My understanding is that this will allow for all letters, in all cases, hyphens, commas and apostrophes along with whitespace at any location in the string.
Quite new to the language so just getting my head around it.
Thanks.
Ok, since it's only given a presumed name, here's what you might want to try:
^[A-Z][A-Za-z'-]+[a-z](,? [A-Z][A-Za-z'-]+[a-z])*$
This will work with names like O'Harry Jefferson-Wayne, but will reject words not ending with a small English letter.
The gist of it is this [A-Z] start of name, [A-Za-z'-]+ continuation, [a-z] end of name. then a repeatable group of optional names, with an optional comma delimiter, but required space.
I took more time writing the examples to test at http://www.regexplanet.com/. That's where I practice my regular expressions when I'm developing. You should try it.

Regex - group name equals up until the next hyphen if present, if not go to the end

I am using Regex to categorise codes in Omniture.
I need to work out a way where I can get my Match Group to go to the next - if present but go to the end if not.
This is an example of a tracking code
KNC-GUK-FUK-GEN-SUP-MRO-ARALDITE-MRO
My current Regex is (which isn't working as desired)
(?i)knc-(.*?)(SUP-)(.*?)(-)(.+)(?= *-|$)
So it needs to have KNC and SUP- and I need to capture the word after the next hypen, in this case ARALDITE.
edit the codes can be such as KNC-GUK-FUK-GEN-SUP-MRO-ARALDITE which is why I have an issue.
Just to clarify, it is the text in the Name in Match Group which I need, not just the match itself.
Is there a way of doing this?
Any help you could offer would be really appreciated.
Thanks
Shani
This matches everything up until the end of ARALDITE:
knc-(.+)SUP-[^-]+-[^-]+
Where the final [^-]+ is matching just ARALDITE. I'm afraid I'm not quite sure what the other parts of your attempt are trying to do, or what captures you want to make, but hopefully you can work from this to a final solution.
[^-] just matches anything that isn't a hyphen, so of course [^-]+ matches either until the end of the string or until it encounters a hyphen.
How about
KNC-.*?SUP-.*?-(.*?)-
Expl.: Make sure "KNC-" is present and followed, at any, point by "SUP-", then skip until next '-' and capture to, not including, the following '-'.
Regards

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

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,})$