regex match pattern in a string multiple times [duplicate] - c#

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

Related

Need Regular expression for the following [duplicate]

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

Validate string for numbers, letters and white space with regex [duplicate]

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

Regular Expression accepting only 9 digits, no special characters , may have leading zero's only up to 2

Hi i need a Regular Expression which should accept exactly 9 characters that to only Numbers from 0-9 ,but should not contain all Zeros ,should not contain special characters , may start with zero and leading zero's must be only up to 2.
Regular expression should accept the below pattern
123456789
012345678
001234567
Right now i have ^[1-9][0-9]{8}$ this regular expression which is accepting 9 digits, no special characters, and should not start with zero.
You can use a lookahead to check leading zero's must be only up to 2 (and should not contain all Zeros in the same step) like
^(?!0{3})[0-9]{9}$
(?!0{3}) discard any number starting with 3 zeros
[0-9]{9}match any other 9 digit number
If RE doesn't have look-ahead then simply build your expression from scratch with all your criteria:
^(0[1-9]{8}|00[1-9]{7}|[1-9][0-9]{8})$
Again, depending on your engine you may need to escape pipes and some of the brackets :)

Regex - At least one alphanumeric character and allow spaces [duplicate]

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 followed by 8 digits

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.

Categories