Regex to accept special characters only with alpha numeric - regex

I created the following regex:
^[a-zA-Z0-9 ']{1,24}\r?$
It should accept alphanumeric, space and apostrophe. And the input should be minimum 1 character and maximum 24 characters.But it accepts inputs with only apostrophe and space also(e.g. " ' ' "). I’m expecting to accept apostrophe and space only with some alphanumeric characters. So below test cases should pass
Pass
Test
Test'My Regex
Test' 123' Regex '
Fail
''

You may use
^(?=.{1,24}$)[a-zA-Z0-9 ']*[A-Za-z][a-zA-Z0-9 ']*$
Or, if strings with just a single digit already make them valid:
^(?=.{1,24}$)[a-zA-Z0-9 ']*[A-Za-z0-9][a-zA-Z0-9 ']*$
See the regex demo
Details
^ - start of string
(?=.{1,24}$) - the whole string must contain 1 to 24 chars
[a-zA-Z0-9 ']* - 0+ alphanumeric, space or ' chars
[A-Za-z] - an alpha char (NOTE replace with [A-Za-z0-9] to also allow strings with just a digit)
[a-zA-Z0-9 ']* - 0+ alphanumeric, space or ' chars
$ - end of string.

Related

Regular expression to allow spaces between words (without special characters) but removing spaces at the beginning

I'm working on validating a field in which you can enter
letter (0-9)
characters (a-Z),
spaces,
'-' and
'_'
but you cannot enter special characters (!##$%)
You can also add more space characters (at the end and beginning)
but after the first spacing must be at least one allowed character
Good:
" some 123 exa_mple m-s-g "
"123abc"
" a"
Bad:
" "
"123!##abc"
You could use a negative lookahead to assert that special characters do not appear anywhere in the input:
^(?!.*[!##$%])\s*[A-Za-z0-9_-][A-Za-z0-9 _-]*$
Demo
Here is an explanation of the pattern used:
^ from the start of the string
(?!.*[!##$%]) assert that no symbols occur anywhere
\s* match optional leading whitespace
[A-Za-z0-9_-] match one allowed character (non space)
[A-Za-z0-9 _-]* then match zero or more allowed characters (including space)
$ end of the string
I' use:
^(?!\h+$)[\w\h-]+$
Explanation:
^ # beginning of string
(?!\h+$) # negative lookahead, make sure we haven't only spaces
[\w\h-]+ # character class, alphanumeric, underscore, space, hyphen
$ # end of string
Demo & explanation

Regex forward slash separator

I am using the below regex expression to ensure string is max 50 characters in length and that each word starts with uppercase letter:
reMatch("Jet Black","^(?=.{0,50}$)(^|^([A-Z][a-z]* +)*([A-Z][a-z]* *)$)")
This works, but I would also like to allow for option to separate words with / character. Example: Jet/Black and Jet / Black with a space in between.
Your suggestions are highly appreciated! Mike.
If you do not care if there may be several spaces, or slahes or intermingled spaces and slashes you may use
^(?=.{0,50}$)(?:[A-Z][a-z]*(?:[ /]+[A-Z][a-z]*)*)?$
See the regex demo.
To only allow spaces and an optional single slash with (white)spaces after use
^(?=.{0,50}$)(?:[A-Z][a-z]*(?:\s*(?:/\s*)?[A-Z][a-z]*)*)?$
See this regex demo
Details
^ - start of string
(?=.{0,50}$) - string should contain only 0 to 50 chars other than linebreak chars (same as (?!.{51}))
(?:[A-Z][a-z]*(?:\s*(?:/\s*)?[A-Z][a-z]*)*)? - an optional sequence of
[A-Z][a-z]* - an uppercase ASCII letter and 0+ lowercase ASCII letters
(?:\s*(?:/\s*)?[A-Z][a-z]*)* - 0 or more sequences of
\s* - 0+ whitespaces
(?:/\s*)? - an optional / and 0+ whitespaces
[A-Z][a-z]* - an uppercase ASCII letter and 0+ lowercase ASCII letters
$ - end of string.

Regex to match if a word starts and end with a letter, have no more than one consecutive non-letter (. *')

I'm currently trying to find a regex to match a specific use case and I'm not finding any specific way to achieve it. I would like, as the title says, to match if a word starts and end with a letter, contains only letter and those characters: "\ *- \'" . It should also have no more than one consecutive non-letter.
I currently have this, but it accepts consecutive non-letter and doesn't accept single letters [a-zA-Z][a-zA-Z \-*']+[a-zA-Z]
I want my regex to accept this string
This is accepted since it contains only spaces and letter and there is no consecutive space
a should be accepted
This is --- not accepted because it contains 5 consecutive non-letters characters (3 dashes and 2 spaces)
" This is not accepted because it starts with a space"
Neither is this one since it ends with a dash -
You may use
^[a-zA-Z]+(?:[ *'-][a-zA-Z]+)*$
See the regex demo and the regex graph:
Details
^ - start of string anchor
[a-zA-Z]+ - 1+ ASCII letters
(?:[ *'-][a-zA-Z]+)* - 0 or more sequences of:
[ *'-] - a space, *, ' or -
[a-zA-Z]+ - 1+ ASCII letters
$ - end of string anchor.

Regular Expression for First name

I've got a RegEx pattern fro user first and last name. They can only contain alphabetic characters, spaces (0 - any ), hyphens (0 - any), apostrophes ' (0 - any ) and number of symbols 1 - 40. First and Last name can not start with ' or whitespace
Here is my pattern code.
^^[a-zA-Z]+[\-\'\s]?[a-zA-Z ]{1,40}$
But RegEx does now allow to use this sample Endevald O'McKnight
You may use
^(?=.{1,40}$)[a-zA-Z]+(?:[-'\s][a-zA-Z]+)*$
See the regex demo.
Details
^ - a start of a string
(?=.{1,40}$) - there must be 1 to 40 chars other than line break chars in the string
[a-zA-Z]+ - 1 or more ASCII letters
(?: - starto of a non-capturing group repeated 0 or more times matching sequences of
[-'\s] - a -, ' or whitespace
[a-zA-Z]+ - 1+ ASCII letters
)* - end of the grouping
$ - end of string

Match words with hyphens and apostrophes

I have the following regex for matching words:
\w+(?:'|\-\w+)?
For the following string:
' 's yea' don't -yeah no- ice-cream '
it gives the following matches:
s yea' don't yeah no ice-cream
However, I would like the following matches:
's yea' don't yeah no ice-cream
Since a word can start or end with an apostrophe but not with a hyphen. Note the a ' on its own should not be matched.
Your \w+(?:'|\-\w+)? starts matching with a word character \w, thus all "words" starting with ' are not matched as per the requirements.
In general, you can match words with and without hyphens with
\w+(?:-\w+)*
In the current scenario, you may include the \w and ' into a character class and use
'?\w[\w']*(?:-\w+)*'?
See the regex demo
If a "word" can only have 1 hyphen, replace * at the end with the ? quantifier.
Breakdown:
'? - optional apostrophe
\w - a word character
[\w']* - 0+ word character or an apostrophe
(?:-\w+)* - 0+ sequences of:
- - a hyphen
\w+ - 1+ word character
'? - optional apostrophe