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?$
Related
I'm trying to modify a fairly basic regex pattern in C# that tests for phone numbers.
The patterns is -
[0-9]+(\.[0-9][0-9]?)?
I have two questions -
1) The existing expression does work (although it is fairly restrictive) but I can't quite understand how it works. Regexps for similar issues seem to look more like this one -
/^[0-9()]+$/
2) How could I extend this pattern to allow brackets, periods and a single space to separate numbers. I tried a few variations to include -
[0-9().+\s?](\.[0-9][0-9]?)?
Although i can't seem to create a valid pattern.
Any help would be much appreciated.
Thanks,
[0-9]+(\.[0-9][0-9]?)?
First of all, I recommend checking out either regexr.com or regex101.com, so you yourself get an understanding of how regex works. Both websites will give you a step-by-step explanation of what each symbol in the regex does.
Now, one of the main things you have to understand is that regex has special characters. This includes, among others, the following: []().-+*?\^$. So, if you want your regex to match a literal ., for example, you would have to escape it, since it's a special character. To do so, either use \. or [.]. Backslashes serve to escape other characters, while [] means "match any one of the characters in this set". Some special characters don't have a special meaning inside these brackets and don't require escaping.
Therefore, the regex above will match any combination of digits of length 1 or more, followed by an optional suffix (foobar)?, which has to be a dot, followed by one or two digits. In fact, this regex seems more like it's supposed to match decimal numbers with up to two digits behind the dot - not phone numbers.
/^[0-9()]+$/
What this does is pretty simple - match any combination of digits or round brackets that has the length 1 or greater.
[0-9().+\s?](\.[0-9][0-9]?)?
What you're matching here is:
one of: a digit, round bracket, dot, plus sign, whitespace or question mark; but exactly once only!
optionally followed by a dot and one or two digits
A suitable regex for your purpose could be:
(\+\d{2})?((\(0\)\d{2,3})|\d{2,3})?\d+
Enter this in one of the websites mentioned above to understand how it works. I modified it a little to also allow, for example +49 123 4567890.
Also, for simplicity, I didn't include spaces - so when using this regex, you have to remove all the spaces in your input first. In C#, that should be possible with yourString.Replace(" ", ""); (simply replacing all spaces with nothing = deleting spaces)
The + after the character set is a quantifier (meaning the preceeding character, character set or group is repeated) at least one, and unlimited number of times and it's greedy (matched the most possible).
Then [0-9().+\s]+ will match any character in set one or more times.
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 need a regular expression validation expression that will
ALLOW
positive number(0-9)
, and .
DISALLOW
letter(a-z)
any other letter or symbol except . and ,
for example, on my asp.net text box, if I type anything#!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.
I've been trying to use this regex:
^\d+(\.\d\d)?$
but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol
Don't Use \d to match [0-9] in .NET
First off, in .NET, \d will match any digits in any script, such as:
654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
So you really want to be using [0-9]
Incomplete Spec
You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match
...,,,
See demo.
Tweaking the Spec
It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?
We could start with something like this, to get some fairly well-formed strings:
^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$
Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.
Sample Matches:
0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22
Sample Non-Matches:
12,
123.
1,1,1,1
Your regex would be,
(?:\d|[,\.])+
OR
^(?:\d|[,\.])+$
It matches one or more numbers or , or . one or more times.
DEMO
Maybe you can use this one (starts with digit, ends with digit):
(\d+[\,\.])*\d+
If you need more sophisticated price Regex you should use:
(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?
Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:
(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
I think the best regex for your condition will be :
^[\d]+(?:,\d+)*(?:\.\d+)?$
this will validate whatever you like
and at the same time:
not validate:
numbers ending in ,
numbers ending in .
numbers having . before comma
numbers having more than one decimal points
check out the demo here : http://regex101.com/r/zI0mJ4
Your format is a bit strange as it is not a standard format.
My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number.
But because of formatting, not sure it would work.
Another way is the regex, of course.
The one you propose means :
"some numbers then possibly a group formed by a dot and two numbers exactly".
This is not what you exepected.
Strictly fitted your example of a number lower than 100,000.99 one regex could be :
^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$
A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+
I'm completely new in this area, I need a regex that follows these rules:
Only numbers and symbols are allowed.
Must start with a number and ends with a number.
Must not contain more than 1 symbol in a row. (for example 123+-4567 is not accepted but 12+345-67 is accepted.
I tried ^[0-9]*[+-*/][0-9]*$ but I think it's a stupid try.
You were close with your attempt. This one should work.
^[0-9]+([+*/-][0-9]+)*$
explanation:
^ matches beginning of the string
[0-9]+ matches 1 or more digits.
[+*/-] matches one from specified symbols
([+*/-][0-9]+)* matches group of symbol followed by at least one digit, repeated 0 or more times
$ matches end of string
We'll build that one from individual parts and then we'll see how we can be smarter about that:
Numbers
\d+
will match an integer. Not terribly fancy, but we need to start somewhere.
Must start with a number and end with a number:
^\d+.*\d+$
Pretty straightforward. We don't know anything about the part in between, though (also the last \d+ will only match a single digit; we might want to fix that eventually).
Only numbers and symbols are allowed. Depending on the complexity of the rest of the regex this might be easier by explicitly spelling it out or using a negative lookahead to make sure there is no non-(number|symbol) somewhere in the string. I'll go for the latter here because we need that again:
(?!.*[^\d+*/-])
Sticking this to the start of the regex makes sure that the regex won't match if there is any non-(number|symbol) character anywhere in the string. Also note that I put the - at the end of the character class. This is because it has a certain special meaning when used between two other characters in a character class.
Must not contain more than one symbol in a row. This is a variation on the one before. We just make sure that there never is more than one symbol by using a negative lookahead to disallow two in sequence:
(?!.*[+/*-]{2})
Putting it all together:
(?!.*[^\d+*/-])(?!.*[+/*-]{2})^\d+.*\d+$
Testing it:
PS Home:\> '123+-4567' -match '(?!.*[^\d+*/-])(?!.*[+/*-]{2})^\d+.*\d+$'
False
PS Home:\> '123-4567' -match '(?!.*[^\d+*/-])(?!.*[+/*-]{2})^\d+.*\d+$'
True
However, I only literally interpreted your rules. If you're trying to match arithmetic expressions that can have several operands and operators in sequence (but without parentheses), then you can approach that problem differently:
Numbers again
\d+
Operators
[+/*-]
A number followed by an operator
\d+[+/*-]
Using grouping and repetition to match a number followed by any number of repetitions of an operator and another number:
\d+([+/*-]\d+)*
Anchoring it so we match the whole string:
^\d+([+/*-]\d+)*$
Generally, for problems where it works, this latter approach works better and leads to more understandable expressions. The former approach has its merits, but most often only in implementing password policies (apart from »cannot repeat any of your previous 30689 passwords«).
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}$");