This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to make sure a first name field has at least one alphanumeric character and also allow spaces and dashes.
**VALID**
David
Billie Joe
Han-So
**INVALID**
-
Empty is also invalid
To ensure the dashes and spaces happen in legitimate places, use this:
(?i)^[a-z]+(?:[ -]?[a-z]+)*$
See demo.
(?i) puts us in case-insensitive mode
^ ensures we're at the beginning of the string
[a-z]+ matches one or more letters
[ -]?[a-z]+ matches an optional single space or dash followed by letters...
(?:[ -]?[a-z]+)* and this is allowed zero or more times
$ asserts that we have reached the end of the string
You mentioned alphanumeric, so in case you also want to allow digits:
(?i)^[a-z0-9]+(?:[ -]?[a-z0-9]+)*$
use this pattern
^(?=.*[a-zA-Z])[a-zA-Z -]+$
Demo
oh, for alphanumeric use
^(?=.*[a-zA-Z0-9])[a-zA-Z 0-9-]+$
Related
This question already has answers here:
Regular expression to validate numbers separated by commas or dashes [closed]
(2 answers)
Closed 2 years ago.
I have to write the Regex expression which accepts - and only numbers either single four digit number or two 4 digit numbers seperated by hyphen as shown below
2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
3945-3974, 3979, 3984-3999
I have used this Regex ^[0-9_,]+ but this line Regex.IsMatch(line, #"^[0-9_,]+$") returns false.
Regards,
Nagasree
The pattern that you tried is not matching as there is no hyphen or space in the character class. But when you would add those, the pattern still does not take any format into account.
You could match 4 digits with optional hyphen and 4 digits part. Then repeat that preceded by a space:
^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$
Regex demo
var s = "2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897";
Console.WriteLine(Regex.IsMatch(s, #"^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$"));
Output
True
This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Match string only if not preceded with another
(1 answer)
Closed 3 years ago.
Reading through stack overflow examples i couldn't find a working solution for the below test case.
I need to match a pattern being tested against a list of strings..
the pattern should match if word1 exist, but word2 doesn't exist before it.
Any character can exist in between.
Examples:
pattern - match if word tty_osc exist and mov_osc doesn't exist anywhere before it.
abd.defg.mov_osc.ccr.tty_osc.val - doesn't match... tty_osc exist but mov_osc also exist before tty_osc
abd.defg.ccr.tty_osc.val - match - tty_osc exist, no mov_osc before
I've tried the following negative lookbehind regex - (?<!mov_osc).*tty_osc
You could use a negative lookahead to assert what is on the right is not your forbidden word followed by the accepted word.
^(?!.*?\bmov_osc\b.*?\btty_osc\b).*?\btty_osc\b.*$
Regex demo
You need to move the .* inside of the negative look behind (?<!mov_osc.*)tty_osc. Otherwise it will match everything before tty_osc and the negative look behind would just check against the beginning of the string.
RegexStorm.Net Demo
This question already has answers here:
C# Regex to match the word with dot
(3 answers)
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 3 years ago.
I have some validation code that checks a string against a regular expression.
Regex regex = new Regex(RegexPattern);
if (!regex.IsMatch(value))
{
errorMessage = "The data is not in the correct format.";
return false;
}
If I set the regular expression pattern to ^[0-9]*.[0-9]*.[0-9]*.[0-9]*$, it correctly accepts 1.0.0.0; however, it also accepts 1.0.0..
How can I modify the pattern so that 1.0.0.0 is accepted but 1.0.0. is rejected?
[0-9]* means 0 or more occurrence of [0-9]
[0-9]+ means 1 or more occurrence of [0-9]
^[0-9]*.[0-9]*.[0-9]*.[0-9]*$
Change * to +:
^[0-9]+.[0-9]+.[0-9]+.[0-9]+$
Just a slight misunderstanding about * and +. The former accepts either no occurrences or more (>=0), the latter only matches if such vocabulary occurs at least once (>=1).
^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
I usually also escape the dot for safety, not entirely sure if it's necessary but I still do it. :)
You can use this pattern:
^\d+\.\d+\.\d+\.\d+$
Explanation:
^ - begin of string
\d - any digit
+ - at least one char
\. - exactly a dot char
$ - end of string
By the way, your input looks like an IP address. If so you can modify your reges like this:
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
{1,3} means: 'from 1 to 3 chars'
Demo: regex101
To match 4 sets of digits separated by a period
^\d+(?:\.\d+){3}$
This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
My password strength criteria is as below :
Character length: 8 -> 24
Contains least 3 of following types:
Uppercase letter [A-Z]
Lowercase letter [a-z]
Numeric [0-9]
Special character ~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/
Can anyone help me to make regular expression and explain. Thanks.
It would be very difficult to do in one regex. Personally, I'd check for each case separately and count if you've got three matches. It would be easier to read and maintain.
So match [A-Z] then [a-z] then [0-9] and finally [~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/]
If you end up with a match in three of the tests that's success.
Use this Regex:
(?=.{8,24})(?=.*\d)(?=.*[A-Za-z])(?=.*[~`!##\$%\^&\*()_=\+[{\]}\|;:'"<,>\.\?/]).*
Debuggex Demo
I want to retrieve numbers from string whose character start from 8 OR 9 OR 6 and is followed by 8 digits.
E.G
sting string string 85689252 91585555 65987582 sting 12345678 string
Output should be:
85689252
91585555
65987582
You probably want this regex:
\b[896]\d{7}\b
I assume that the numbers are at least separated by some character other than alphanumeric and _, so I used \b to match word boundary.
If you want to relax the condition, the regex engine must support look ahead and look behind:
(?<!\d)[896]\d{7}(?!\d)
In this case, I just make sure the whole number is not preceded or followed by any digit.
Your question says followed by 8 digits, but it seems you mean the whole number should have 8 digits - so I changed my answer according to your sample input.
Use Regex.Matches to get all the string that matches the regex.