I need to match a string under the following conditions using Regex in C#:
- Entire string can only be alphanumeric (including spaces).
- Must be a maximum of 15 characters or less (including spaces).
- First & last characters can only be a letter.
- A single space can appear multiple times in anywhere but the first and last characters of the string. (Multiple spaces together should not be allowed).
- Capitalization should be ignored.
- Should match the WHOLE word(s).
If any one of these preconditions are broken, a match should not follow.
Here is what i currently have:
^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$
And here are some test strings that should match:
- Stack OverFlow
- Iamthe greatest
- A
- superman23s
- One Two Three
And some that shouldn’t match (note the spaces):
- Stack [double_space] Overflow Rocks
- 23Hello
- ThisIsOver15CharactersLong
- Hello23
- [space_here]hey
etc.
Any suggestions would be much appreciated.
You should use
lookaheadsyou can try it here