This question already has answers here:
.NET RegEx for letters and spaces
(5 answers)
Closed 4 years ago.
I need to save in a string only numbers and letters and white spaces. I need to use System.Text.RegularExpressions.Regex. If I put the string in a IF clause, it will only be valid if it contains only numbers, letters and white spaces
This should do the trick
if (Regex.IsMatch(yourstring, #"^[\d \w \s]+$"))
{
Console.WriteLine("passed");
}
Explanation:
^: designates the beginning of a string
$: designates the end of a string
[...] matches all caracters inside the parantheses
\d matches digits
\w matches letters
\s matches spaces
+ means 1 or more occurences
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:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I'm trying to design a RegEx in C# to match 4 digits and 6 characters in a string.
string str = "Hello World! ABCD112233 fsdf sdfsdf 234324 fdsfds 4234 efwedf34ref dfsdf34f34f";
that matches only ABCD112233 in the above string.
Regex regex = new Regex("^[A-Za-z]{4}[0-9]{6}$", RegexOptions.Multiline);
How do I solve this problem?
You would use the {n} quantifier to match a certain number of characters like so: [A-Za-z]{4}[0-9]{6}. This will match 4 letters (A-Z, a-z) and then 6 digits (0-9).
Note: don't use ^ and $ at the start and end as then it will only match if the whole word matches the regex.
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I have the following string "*&abc=123&**&defg=hh&*" and so on the pattern start and end is && and I would like to have the following when I do regex.matches or regex.split
first match = &abc=123&
2 match = &defg=hh&
appreciate any help
&[0-9a-zA-Z]+=[0-9a-zA-Z]+&
You may need to change the [0-9a-zA-Z] depending on what characters you want to allow.
& matches the characters & literally
[0-9a-zA-Z]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
= matches the character = literally
[0-9a-zA-Z]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
& matches the characters & literally
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-]+$
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.