Using a regular expression, I would like to match the following string:
4232,2232
I have tried
^[0-9]+(,[0-9]+)$
However, it doesn't work as expected. I want to cater for 4 numbers, a comma and 4 numbers.
You can use the following:
\d{4},\d{4} //or ^\d{4},\d{4}$ with anchors for start and end of string
Explanation:
\d{4} match digits exactly 4 times (\d is shorthand notation for [0-9])
,\d{4} followed by comma and exactly 4 digits again
If i understand you correctly the RegEx you are looking for is basically:
(\d{4},\d{4})
This matches your provided expression as one group.
As an alternative you could write:
([0-9]{4},[0-9]{4})
which has the same result.
In C#, you can use Regex.IsMatch with the \b\d{4},\d{4}\b regex:
var found_value1 = Regex.IsMatch("4232,2232", #"\b\d{4},\d{4}\b");
var found_value2 = Regex.IsMatch("12345,2232", #"\b\d{4},\d{4}\b");
\b makes sure we match whole number.
Output:
true
false
Related
I have to check whether the user entered string is in a particular format as below eg:
123-1234-1234567-1
ie after first 3 digit a hyphen, then after 4 digit another hyphen, after seven digit an hyphen then a single digit.
I used the below regular expression
#"^\(?([0-9]{3})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{7})[-. ]?([0-9]{1})$"
It is working fine for above expression but it will also pass the expression without - also
eg:- 123-1234-1234567-1 //pass
123123412345671 //also getting pass.
The second string should fail. What change i should do in the regular expression to achieve the same?
You can simply use:
^\d{3}-\d{4}-\d{7}-\d$
If you want to allow dot and space also as delimiter then use:
^\d{3}[-. ]\d{4}[-. ]\d{7}[-. ]\d$
The problems is that you are having optional quantifier ? after [. ].
Remove them, and it should work fine
#"^\(?([0-9]{3})\)?[-. ]([0-9]{4})[-. ]([0-9]{7})[-. ]([0-9]{1})$"
Regex demo
The ? makes the preceding pattern optional as it matches 0 or 1 character. So in the second example the regex engine safely matches zero - to get to match the entire string
I am trying to create regular expression for following type of strings:
combination of the prefix (XI/ YV/ XD/ YQ/ XZ), numerical digits only, and either no ‘Z’ or a ‘Z’ suffix.
For example, XD35Z should pass but XD01HW should not pass.
So far I tried following:
#"XD\d+Z?" - XD35Z passes but unfortunately it also works for XD01HW
#"XD\d+$Z" - XD01HW fails which is what I want but XD35Z also fails
I have also tried #"XD\d{1,}Z"? but it did not work
I need a single regex which will give me appropriate results for both types of strings.
Try this regex:
^(XI|YV|XD|YQ|XZ){1}\d+Z{0,1}$
I'm using quantifying braces to explicitly limit the allowed numbers of each character/group. And the ^ and $ anchors make sure that the regex matches only the whole line (string).
Broken into logical pieces this regex checks
^(XI|YV|XD|YQ|XZ){1} Starts with exactly one of the allowed prefixes
\d+ Is follow by one or more digits
Z{0,1}$ Ends with between 0 and 1 Z
You're misusing the $ which represents the end of the string in the Regex
It should be : #"^XD\d+Z?$" (notice that it appears at the end of the Regex, after the Z?)
The regex following the behaviour you want is:
^(XI|YV|XD|YQ|XZ)\d+Z?$
Explanation:
combination of the prefix (XI/ YV/ XD/ YQ/ XZ)
^(XI|YV|XD|YQ|XZ)
numerical digits only
\d+
‘Z’ or a ‘Z’ suffix
Z?$
I need to write the regular expression to fit a specific code pattern: the string to match has 18 integer characters and I need to check if in the first position there is 0 and in the second one 8 or 9. I wrote this expression, but it doesn't work:
Regex regex = new Regex(#"^(.{0}[0]{1}[8,9])(^\d{18}$)");
string compare = "082008014385161873";
if (regex.IsMatch(compare))
{
//true
}
Anyone can help me?
Try this regular expression. It does the following:
Verifies first digit as 0
Verifies second digit as a 8 or 9
Verifies that 16 more digits follow the first 2 digits
.
^0[89]\d{16}$
Use following regular expression:
^0[89]\d{16}$
Alternative using positive lookahead assertion:
^(?=0[89])\d{18}$
I have the following regex:
Regex pattern = new Regex(#"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}/(.)$");
(?=.*\d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,20} //should contain at least 8 characters and maximum of 20
My problem is I also need to check if 3 consecutive characters are identical. Upon searching, I saw this solution:
/(.)\1\1/
However, I can't make it to work if I combined it to my existing regex, still no luck:
Regex(#"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/(.)\1\1/");
What did I missed here? Thanks!
The problem is that /(.)\1\1/ includes the surrounding / characters which are used to quote literal regular expressions in some languages (like Perl). But even if you don't use the quoting characters, you can't just add it to a regular expression.
At the beginning of your regex, you have to say "What follows cannot contain a character followed by itself and then itself again", like this: (?!.*(.)\1\1). The (?! starts a zero-width negative lookahead assertion. The "zero-width" part means that it does not consume any characters in the input string, and the "negative lookahead assertions" means that it looks forward in the input string to make sure that the given pattern does not appear anywhere.
All told, you want a regex like this:
new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$")
I solved by using trial and error:
Regex pattern = new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$");
I am building a user registration form using C# with .NET.
I have a requirement to validate user entered password fields.
Validation requirement is as below.
It should be alphanumeric (a-z , A-Z , 0-9)
It should accept 6-10 characters (minimum 6 characters, maximum 10 characters)
With at least 1 alphabet and number (example: stack1over)
I am using a regular expression as below.
^([a-zA-Z0-9]{6,10})$
It satisfies my first 2 conditions.
It fails when I enter only characters or numbers.
Pass it through multiple regexes if you can. It'll be a lot cleaner than those look-ahead monstrosities :-)
^[a-zA-Z0-9]{6,10}$
[a-zA-Z]
[0-9]
Though some might consider it clever, it's not necessary to do everything with a single regex (or even with any regex, sometimes - just witness the people who want a regex to detect numbers between 75 and 4093).
Would you rather see some nice clean code like:
if not checkRegex(str,"^[0-9]+$")
return false
val = string_to_int(str);
return (val >= 75) and (val <= 4093)
or something like:
return checkRegex(str,"^7[5-9]$|^[89][0-9]$|^[1-9][0-9][0-9]$|^[1-3][0-9][0-9][0-9]$|^40[0-8][0-9]$|^409[0-3]$")
I know which one I'd prefer to maintain :-)
Use positive lookahead
^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,10}$
Look arounds are also called zero-width assertions. They are zero-width just like the start and end of line (^, $). The difference is that lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.
The syntax for look around:
(?=REGEX) Positive lookahead
(?!REGEX) Negative lookahead
(?<=REGEX) Positive lookbehind
(?<!REGEX) Negative lookbehind
string r = #"^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{6,10}$";
Regex x = new Regex(r);
var z = x.IsMatch(password);
http://www.regular-expressions.info/refadv.html
http://www.regular-expressions.info/lookaround.html