I would like to know how to write a regex in c# that allows only
numbers, letters, spaces and the # symbol.
Valid inputs are:
Abc
Abc def
#Abc
Abc#
Example I tried so far: #"[^\w-\s-#]"
You may use
#"^[A-Za-z0-9\s#]*$"
See the regex demo
The pattern matches:
^ - start of the string
[A-Za-z0-9\s#]* - zero or more (*, if you do not want to match an empty string, use +, 1 or more) occurrences of ASCII letters, digits, any whitespace and # chars
$ - end of string (replace with \z if you do not want to match if the matching string ends with \n char).
Related
I am trying to build regex to match - Test get:all words:test
can start with a word then space and followed by any occurrence of word:word separated by space.
#"^[a-zA-Z]+/s(^[a-zA-Z]+:^[a-zA-Z]+/s)*"
You added extra start of string anchors, ^, inside the pattern, and you need to remove them for sure.
Besides, the whitespace patterns must be written as \s and the first \s must be moved inside the repeated group that should be converted into a non-capturing one ((?:...)) for better performance.
You can use
^[a-zA-Z]+(?:\s+[a-zA-Z]+:[a-zA-Z]+)*$
See the regex demo. Details:
^ - start of string
[a-zA-Z]+ - one or more ASCII letters
(?:\s+[a-zA-Z]+:[a-zA-Z]+)* - zero or more repetitions of
\s+ - one or more whitespaces
[a-zA-Z]+:[a-zA-Z]+ - one or more ASCII letters, :, one or more ASCII letters
$ - end of string (or use \z to match the very end of string).
If you meant to allow any word chars (letters, digits, connector punctuation) then replace each [a-zA-Z] with \w.
If you need to support just any Unicode letters, replace each [a-zA-Z] with \p{L}.
I recently was assigned an impossible task (in my estimation) to create a regex pattern in which I should be able to validate several words in the same sentence or textbox with the following guidelines:
Each name/word has to have first letter upper case
Names/Words separated by spaces
Each name/word 3 characters long or more
And the sentence or textbox text can't be longer than 20 characters
Example: Joseph Gordon Levitt
This example is exactly 20 characters long, each name (or word) is longer than 3 characters, separated by spaces, and the first letter of each name (or word) is upper case.
I tried this regex pattern ^[A-Z]{1}[a-zA-Z\s]{3,20}$. It works for some strings, but not all.
One of options is this:
^(?!.{21})[A-Z][a-z]{2,}(\s[A-Z][a-z]{2,})*$
Demo: https://dotnetfiddle.net/oWjSI4
Let's walk through the requirements:
Each name/word has to have first letter upper case: Use \p{Lu}
Names/Words separated by spaces: Use \s+ (1 or more spaces) / \s (only single space)
Each name/word 3 characters long or more: Word pattern will thus be \p{Lu}\p{L}{2,} - starting with an uppercase and then having 2 or more letters
And the sentence or textbox text can't be longer than 20 characters: Use a positive lookahead right after ^ / \A (start of string): (?!.{21}) or (?=.{0,20}$).
The resulting regex will look like
^(?!.{21})\p{Lu}\p{L}{2,}(?:\s\p{Lu}\p{L}{2,})*$
^(?=.{0,20}$)\p{Lu}\p{L}{2,}(?:\s\p{Lu}\p{L}{2,})*$
Or, if there can be 1+ whitespaces between words
^(?!.{21})\p{Lu}\p{L}{2,}(?:\s+\p{Lu}\p{L}{2,})*$
^(?=.{0,20}$)\p{Lu}\p{L}{2,}(?:\s+\p{Lu}\p{L}{2,})*$
NOTE: If you ever test it against a string that can end with a \n, newline char, replace $ with \z.
See the regex demo.
Details
^ - start of string
(?=.{0,20}$) - there must be 0 to 20 non-newline chars in the string till the end
\p{Lu} - an uppercase letter
\p{L}{2,} - two or more letters
(?:\s\p{Lu}\p{L}{2,})* - 0 or more repetitions of:
\s - a whitespace (or 1+ whitespaces if \s+ is used)
\p{Lu}\p{L}{2,} - an uppercase letter and then any two or more letters
$ - end of string (\z is the very end of the string).
I have a string like Acc:123-456-789 and another string like -1234567, I need your help to write an expression to match digits in case there is no separator between the digits.
-*(?!\d*(?:\d*-)$)\d*$
Input strings:
Acc:123-456-789 -12323232 7894596
Desired result:
group 1 12323232
group 2 7894596
I think this ought to work:
(?<=^|\s|\s-)(\d+)(?=\s|$)
Breaking it down:
(?<=^|\s|\s-) - A positive lookbehind that matches the start of the string, whitespace, or whitespace followed by a -.
(\d+) - Matches and captures number sequences.
(?=\s|$) - A positive lookahead that matches whitespace or the end of the string.
** Note: If you need to capture negative number sequences, replace (\d+) with (\-?\d+).
Try it online
Regex reference
Remember for use in C# that you need to escape backslashes or use the # prefix to a string literal (#" ").
I am trying to match the following pattern.
A minimum of 3 'groups' of alphanumeric characters separated by a hyphen.
Eg: ABC1-AB-B5-ABC1
Each group can be any number of characters long.
I have tried the following:
^(\w*(-)){3,}?$
This gives me what I want to an extent.
ABC1-AB-B5-0001 fails, and ABC1-AB-B5-0001- passes.
I don't want the trailing hyphen to be a requirement.
I can't figure out how to modify the expression.
Your ^(\w*(-)){3,}?$ pattern even allows a string like ----- because the only required pattern here is a hyphen: \w* may match 0 word chars. The - may be both leading and trailing because of that.
You may use
\A\w+(?:-\w+){2,}\z
Details:
\A - start of string
\w+ - 1+ word chars (that is, letters, digits or _ symbols)
(?:-\w+){2,} - 2 or more sequences of:
- - a single hyphen
\w+ - 1 or more word chars
\z - the very end of string.
See the regex demo.
Or, if you do not want to allow _:
\A[^\W_]+(?:-[^\W_]+){2,}\z
or to only allow ASCII letters and digits:
\A[A-Za-z0-9]+(?:-[A-Za-z0-9]+){2,}\z
It can be like this:
^\w+-\w+-\w+(-\w+)*$
^(\w+-){2,}(\w+)-?$
Matches 2+ groups separated by a hyphen, then a single group possibly terminated by a hyphen.
((?:-?\w+){3,})
Matches minimum 3 groups, optionally starting with a hyphen, thus ignoring the trailing hyphen.
Note that the \w word character also select the underscore char _ as well as 0-9 and a-z
link to demo
I WOuld like to implement textBox in which user can only insert text in pattern like this:
dddddddddd,
dddddddddd,
dddddddddd,
...
where d is a digit. If user leave control with less then 10 digits in a row validation should fail and he should not be able to write in one line more than 10 digits, then acceptable should be only comma ",".
Thanks for help
Match m = Regex.Match(textBox.Text, #"^\d{10},$", RegexOptions.Multiline);
Haven't tried it, but it should work. Please take a look here and here for more information.
I suggest the regex
\A(?:\s*\d{10},)*\s*\d{10}\s*\Z
Explanation:
\A # start of the string
(?: # match the following zero or more times:
\s* # optional whitespace, including newlines
\d{10}, # 10 digits, followed by a comma
)* # end of repeated group
\s* # match optional whitespace
\d{10} # match 10 digits (this time no comma)
\s* # optional whitespace
\Z # end of string
In C#, this would look like
validInput = Regex.IsMatch(subjectString, #"\A(?:\s*\d{10},)*\s*\d{10}\s*\Z");
Note that you need to use a verbatim string (#"...") or double all the backslashes in the regex.