Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need an regular expression pattern to only accept positive whole numbers. It can also accept a single zero.
I do not want to accept decimals, negative number and numbers with leading zeros.
Any suggestions?
^(0|[1-9][0-9]*)$
"[1-9][0-9]*|0"
I'd just use "[0-9]+" to represent positive whole numbers.
This will allow decimal numbers (or whole numbers) that don't start with zero:
^(([1-9]*)|(([1-9]*)\.([0-9]*)))$
If you want to allow numbers that start with zero, you can do :
^(([0-9]*)|(([0-9]*)\.([0-9]*)))$
/([1-9][0-9]*)|0/
/^0|[1-9]\d*$/
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing regex to match the pattern like this.
abc:123-12-4
abc: It should be exact match including colon
123 Number match any length
- Exact Match
12 Number Match any length
- Exact match
4 Exact match
Any ideas how it can be done in a simpler way.
You can use this regex:
^abc:[0-9]+-[0-9]+-4$
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I write a Reg-ex Expression to check whether a string is a binary multiple of 4? I am not good at making DFA and finding expressions.
A multiple of 4 in binary is any binary number that ends with 00, so this regexp should do it:
^(?:[10]*00|00?)$
If you mean a multiple of 4 in decimal, I wouldn't do that with a regexp, except perhaps to verify that it's a number. Then I'd parse it and check whether number % 4 is zero.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need a regular expression to only match rising numbers.
ex: 22335566 66678
but not: 444663 33997777666664
the length of the number is not fixed nor is the starting digit.
Any help please?
The best way to implement this using regex is:
^(?=\d)1*2*3*4*5*6*7*8*9*$
The regex matches:
0 or more 1's, followed by,
0 or more 2's, followed by,
0 or more 3's... and so on.
(?=\d) ensures that there is atleast 1 digit in your string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can a regex pattern enforce the following combination of constraints on a numeric value?
the number must be >= 1 and <= 999
(decimal point cannot be the first character in the string?)
the number can be an integer or a number with a fractional component
when it has a fractional component,
no more than 2 digits to the right of the decimal point
EDIT: but at least one digit to the right
must not have leading zero(s)
Perl syntax:
^+?(?:(?:999(?:\.0{1,2})?)|(?:(?!999)[1-9]\d{0,2}(?:\.\d{1,2})?))$
I'll go with this one:
/^
(?:
999.0* # 999.000
| # or
[1-9]\d{,2}((?<!999)\.\d+)? # 1-998 plus optional .\d*
)
$/x
Yes..
^(?!999[.](0*[1-9]+$))(?!0|[.])\d{1,3}([.]\d{1,2})?$
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
How would I go about writing a regex to match the first example, but not the second?
Thank you --
Try
\[Test\]
You need to escape the [ and ] as they indicate range start/end.
[] are special characters for a regex, used for defining a character class, therefore escape them with backslashes.
\[Test\]
If you use [Test], it will match a single character which is either T, e, s or t.