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.
Related
CURRENTLY
I am try to match valid company names from strings with 4 conditions:
the name can ONLY contain alphanumeric characters + spaces + hyphens
the name can contain a hyphen (inside the name)
there are company suffixes that should be excluded from the company name i.e. Pty Ltd, Pty. Ltd., Limited, and Ltd.
If there are additional matches on the same line, these are to be excluded
What I am trying to achieve:
My regex so far:
(?:\s|^)([a-zA-Z0-9]+[a-zA-Z0-9\s-]*?[a-zA-Z0-9]+)(?: Pty Ltd| Ltd(\.){0,1}| Limited){0,1}(?:\s|$)
ISSUES
https://regex101.com/r/Gpbdln/4
It seems I am struggling with:
Excluding the suffixes to be ignored
Making the capture include spaces for the company name (while at the same time excluded suffixes)
I have been stuck on this for over an hour and would appreciate some help.
You may use
^[a-zA-Z0-9]+(?:[\s-]+[a-zA-Z0-9]+)*?(?=(?:\s+(?:(?:Pty\.?\s+)?Ltd\.?|Limited|[a-zA-Z0-9]*[^a-zA-Z0-9\s]).*)?$)
See the regex demo
If you only need to get matches that do not span across lines, replace \s with \h or [\p{Zs}\t] if supported, or [^\S\r\n], to only match horizontal whitespaces.
Details
^ - start of string
[a-zA-Z0-9]+ - 1+ ASCII alphanumeric chars
(?:[\s-]+[a-zA-Z0-9]+)*? - 0 or more (but as few as possible) occurrences of
[\s-]+ - 1+ whitespaces or hyphens
[a-zA-Z0-9]+ - 1+ ASCII alphanumeric chars
(?=(?:\s+(?:(?:Pty\.?\s+)?Ltd\.?|Limited|[a-zA-Z0-9]*[^a-zA-Z0-9\s]).*)?$) - immediately to the right, there must be
(?:\s+(?:(?:Pty\.?\s+)?Ltd\.?|Limited|[a-zA-Z0-9]*[^a-zA-Z0-9\s]).*)? - an optional occurrence of a sequence of patterns:
\s+ - 1+ whitespaces
(?:(?:Pty\.?\s+)?Ltd\.?|Limited|[a-zA-Z0-9]*[^a-zA-Z0-9\s]) - any of
(?:Pty\.?\s+)?Ltd\.?| - an optional sequence of Pty, an optional dot and then 1+ whitespaces and then Ltd string and an optional . char, or
Limited| - Limited string, or
[a-zA-Z0-9]*[^a-zA-Z0-9\s] - any 0 or more ASCII alphanumeric chars followed with a char other than whitespace and alphanumeric char
.* - the rest of the string
$ - end of string.
I'm working with an SAP application called information steward and creating a rule where names will have to be in title case (ie each word is capitalized).
I've formulated the following rule:
BEGIN
IF(match_regex($name, '(^(\b[A-Z]\w*\s*)+$)', null)) RETURN TRUE;
ELSE RETURN FALSE;
END
Although it is successful it appears to accept inputs which should be identified as 'FALSE'. Please see the attached screenshot.
'TesT Name' and 'TEST NAME' should be FALSE but are instead passing under this regex.
Any help/guidance with the regex would be very useful.
The (^(\b[A-Z]\w*\s*)+$) regex presents a pattern that matches a string that fully matches:
^ - start of string
(\b[A-Z]\w*\s*)+ - 1 or more occurrences (due to (...)+) of
\b - a word boundary
[A-Z] - an uppercase ASCII letter
\w* - 0 or more letters/digits/underscores
\s* - 0+ whitespaces
$ - end of string.
As you see, it allows trailing whitespace, and \w matches what [A-Za-z0-9_] matches, i.e. it matches both lower- and uppercase letters.
You want to only match lowercase letters after initial uppercase ones, also allowing - and _ chars. You may use
^[A-Z][a-z0-9_-]*(\s+[A-Z][a-z0-9_-]*)*$
See the regex demo.
Details
^ - start of string anchor
[A-Z][a-z0-9_-]* - an uppercase letter followed with 0+ lowercase letters, digits, _ or - chars
(\s+[A-Z][a-z0-9_-]*)* - zero or more occurrences of:
\s+ - 1 or more whitespaces
[A-Z][a-z0-9_-]* - an uppercase letter followed with 0+ lowercase letters, digits, _ or - chars
$ - end of string.
I would write your regex as:
^[A-Z]\w*(?:\s+[A-Z]\w*)*$
This says to match a single word starting with a capital letter, then followed by one or more spaces and another word starting with a capital, this quantity zero or more times.
I phrase a matching word as starting with [A-Z] followed by \w*, meaning zero or more word characters. This allows for things like A to match.
Demo
Edit:
Based on the comments above, if you want some other character class to represent what follows the initial uppercase letter, then do that instead:
^[A-Z][something]*(?:\s+[A-Z][something]*)*$
where [something] is your character class.
Building a regex to match passwords stored in plain text.
8-15 characters, must contain at least:
1 uppercase letter [A-Z]
1 lowercase letter [a-z]
1 number \d
1 special character [!##\$%\^&\*]
The problem I have is when the password is inline with other text or spaces after, it doesn't return a match. When it's on its own without spaces it matches.
Example:
This is a Testing!23 surrounded by other text.
Testing!23
(?=.{8,15})(?=.*[!##\$%\^&\*])(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*
You want to find all non-whitespace chunks matching the conditions you outlined.
Use
(?<!\S)(?=\S{8,15}(?!\S))(?=[^!##$%^&*\s]*[!##$%^&*])(?=[^\s\d]*\d)(?=[^\sa-z]*[a-z])(?=[^\sA-Z]*[A-Z])\S+
See the regex demo
Details
(?<!\S) - a whitespace or start of string should be right before the current position
(?=\S{8,15}(?!\S)) - right after the current position, there must be 8 to 15 non-whitespace chars followed with either whitespace or end of string
(?=[^!##$%^&*\s]*[!##$%^&*]) - there must be a char from the [!##$%^&*] set after zero or more non-whitespace chars outside of the set
(?=[^\s\d]*\d) - there must be a digit after 0+ non-whitespace and non-digit chars
(?=[^\sa-z]*[a-z]) - 1 lowercase letter must appear after 0+ chars other than whitespace and lowercase letters
(?=[^\sA-Z]*[A-Z]) - 1 uppercase letter must appear after 0+ chars other than whitespace and uppercase letters
\S+ - all checks are over, and if they succeed, match and consume 1+ non-whitespace chars (finally).
I want my regex to allow alphanumeric characters, "/_-" and white spaces in between but it must always have at least one alphanumeric character.
my validation goes like this,
/^([A-Za-z0-9/-]+[A-Za-z0-9/-\s]*[A-Za-z0-9/_-]+)$/
It should accept **ABC_1-2-3 but it must not allow 123 or -_/ alone
Can somebody help me please.
The below given regex will capture strings with alpha-numeric characters with optional white space, hyphen and underscore in it. Try it.
([*A-Za-z]+(\s+)?([\d\-_]+)?)
Your regex is almost right, you need to add 2 positive lookaheads at the start to require at least 1 letter and at least 1 digit:
/^(?=.*[a-z])(?=.*\d)[a-z0-9\/_-][a-z0-9\/_\s-]*[a-z0-9\/_-]$/i
See the regex demo (in the demo, \s is replaced with a space since the demo is multiline).
Details:
^ - start of string
(?=.*[a-z]) - after any 0+ chars other than line break chars, there must be at least 1 letter (replace .* with [^a-z]* for better performance)
(?=.*\d) - after any 0+ chars other than line break chars, there must be at least 1 digit (replace.with\D` for better performance)
[a-z0-9\/_-] - a letter, digit, /, _ or -
[a-z0-9\/_\s-]* - 0+ letters, digits, /, whitespaces, _ or -
[a-z0-9\/_-] - a letter, digit, /, _ or -
$ - end of string.
The i modifier makes the pattern case insensitive.
Does anybody know any regex to restrict a string to specified number of lines and words for data validation of a field in google forms?
I have already tried the following expression
^(?:\b\w+\b[\s\r\n]*){1,250}$
That would limit to 250 words over multiple lines.
but it is not working when there is any special character in the string.
Any help would be appreciated.
Your [\s\r\n] matches any whitespace chars only. You may use \W to match any non-word char:
^\w+(?:\W+\w+){0,249}$
^^
Details:
^ - start of string
\w+ - 1+ word chars
(?:\W+\w+){0,249} - 0 to 249 (1 to 250 in total) sequences of:
\W+ - 1+ non-word chars
\w+ - 1+ word chars (that is, words separated with 1+ symbol, whitespace or punctuation)
$ - end of string.