regular expression on double(10,2) - regex

I have this regularexpression
[0-9]+(,[0-9][0-9]?)?
it matches on 345563,24 but how can I limit the left side part on 8 characters?
88888888,00 - true because 8 characters
999999999,00 - false because 9 characters

Use this:
[0-9]{0,8}(,[0-9][0-9]?)?
{m,n} indicates the minimum and maximum number of occurrences of the previous character/group. You can indicate just a minimum or just a maximum by leaving one side of the expression blank. So the expression above would allow 0 to 8 occurrences of a digit. If you want 1 to 8 occurrences of a digit at the beginning of your expression, use this:
[0-9]{1,8}(,[0-9][0-9]?)?

I would do :
^[1-9][0-9]{,7}(,[0-9][0-9]?)?$
number (10,2) would be matched
empty string won't be matched
first digit should not be 0 (zero)

Borrowing elements from the answers of both #Kent and #user1775603, I'd try the following:
^([1-9][0-9]{0,7}|0)(,[0-9][0-9])?$
This will match:
any up-to-eight-digits number starting with a nonzero, with zero or two decimal digits after the comma
any number >= 0 and < 1 with zero or two decimals after the comma (matching 0,xx where x are digits)
Do note that the decimal separator, unless you take care to do otherwise, is very often locale-dependent. Exactly how to counteract this depends on what language and/or framework you are working with, but it's something to watch out for.

I would recommend \b instead of ^ and $ as in:
\b([1-9][0-9]{0,7}|0)(,[0-9][0-9]?)?\b
So that the input does not need to be fed alone and can be found in the middle of a large text too.

Related

Regular Expression that needs to match three exact digits in a four digit number

The regular expression that I am trying to create should match all numbers that contain three '8's in any 4 digit number. The regular expression that I have only matches the first 10 numbers out of the list of 15 numbers. Any suggestions will be greatly appreciated.
\b[0-9]*(?:8[0-9]*[0-9]?8|8[0-9]*[0-9]?8|8[0-9]*[0-9]?8)\b
Test data:
8088 8188 8288 8388 8488 8808 8818 8828 8838 8848 8880 8881 8882 8883 8884
The last five numbers should also match, but don't.
You can use
\b(?=\d{4}\b)(?:[0-79]*8){3}[0-79]*\b
See the regex demo.
Details:
\b - a word boundary
(?=\d{4}\b) - there must be 4 digits immediately on the right and they should be followed with a word boundary
(?:[0-79]*8){3} - three occurrences of any 0 or more digits but 8 and then 8
[0-79]* - any 0 or more digits but 8
\b - word boundary.
If it's guaranteed that the number is a four-digit number, then you can try the following:
\b8*[0-79]8*\b
To analyze what each part matches, you can check using,
\b(8*)[0-79](8*)\b
This should do it. This will match any of the 4 patterns.
([\d888]|[8\d88]|[88\d8]|[888\d])
You may want to add a check for the delimiter (in your example the space) as this pattern will match across the spaces giving you many more results
\b(\d?8{3}\d?)\b
this makes the first and last digit in the word bound optional, use
either ? or {0,1}
add quantifier to your eight to have exactly
number of eights you need {3}
replace [0-9] with \d as
Digit for brewity
supposed you have only numbers of length 4. Otherwise use an alternative without optional digits: \b(\d8{3}|8{3}\d)\b

Why is my regular Expression that ignore the order of the characters does not work?

I want to make a string pattern that is:
at least 7 characters long
have at least 1 digits, max 5
have at least 3 capital alphabetic characters , max 5
have at least 1 lower alphabetic characters , max 5
have at least 1 special characters , max 5
How to express this in a regular expression?
I can do something like
^((?=.*[A-Z]{3,5})(?=.*[a-z]{1,5})(?=.*[0-9]{1,5})(?=.*[.~!##$%^_&-]{1,5}))(?=.{7,20}).*$
I don't want to require this kind of order. In fact, any mixed order should be accepted, only require the number of characters.
This Match:
PASSW120P45ccb^&#%#
But this one does not
PA12S1SW2045ccb^&#%#
How can i fix this?
P&#Ass120W45ccb^%#
P&#Ass20W45cb^%#
Please have a look at https://regex101.com/r/vF2yO7/51
You need to operate with the contrary character classes, put these into non-capturing groups and repeat these:
^
(?=(?:\D*\d){1,5})
(?=(?:[^A-Z]*[A-Z]){3,5})
(?=(?:[^a-z]*[a-z]){1,5})
(?=(?:[^.\~!##$%^_&-]*[.\~!##$%^_&-]){1,5})
.{7,20}
$
See a demo on regex101.com.
The structure here is always the same, e.g. with the numbers: require anything not a number zero or more times, followed by a number and repeat the whole pattern 1-5 times. In general:
(?=(?:not_what_you_want*what_you_want){min_times, max_times})
In the expression above, all pos. lookaheads follow this scheme, [^...] negates the characters to be matched in the class and \D* is essentially the same as [^\d]*.

regex minus numbers

I want to validate number that don't contain the minus char. The number > 0.
Have you got a regex for that ?
Exclusivly non-negativ numbers with decimal-point: ^\d+(?:.\d+)?$, or capturing with negativ look-behind ((?<!-)[[:digit:]]+) or a myriad other ways depending on the flavour of regex you need and the real problem at hand.
To match absolutes
^\d+$
https://regex101.com/r/O4nGl5/2
To match decimals
^\d+(\.?\d+)?$
https://regex101.com/r/O4nGl5/3
There multiple ways to do that, one of them is:
^[0-9]+$ (for integer numbers)
It checks your input against:
Starts and ends with an integer (^ for beginning and $ for end)
characters between between 0 and 9 (integers)
1 or more occurence (+ for 1 or more occurrences of the previous expression)

Why is the regex to match 1 to 10 written as [1-9]|10 and not [1-10]?

Why is the regex to match numbers from 1 to 10 commonly written as follows?
[1-9]|10
Instead of:
[1-10]
Or this:
[1-(10)]
Sometime a good drawing worth 1000 words...
Here are the three propositions in your question and the way a regex flavour would understand them:
[1-9]|10
[1-10]
[1-(10)]
Invalid regexp !!
This regex is invalid because a range is opened (1-) with a digit but not closed with another digit (ends with ().
A range is usually bound with digits on both sides or letters on both sides.
Images generated with Debuggex
That is because regexes work with characters, not with numbers. [1-9] is equivalent to (?:1|2|3|4|5|6|7|8|9) while [1-10] would be (?:1|0) (because it's the range 1–1 and the digit 0).
Simply put, ranges in character classes always refer to contiguous ranges of characters, despite how they look. Even if they're digits that doesn't mean there is any kind of numeric range.
[1-9]|10
In this:
[1-9] accepts any character from 1 through 9;
| performs an "or" operation;
10 accepts the 10 literally.
[1-10]
This accepts:
any single character between 1 and 1,
or 0.
No matter what pattern is inside [...] (character class), it only matches a single character.
The way the range operator (-) inside character class works is it takes a single character as left operand, and a single character as right operand, then expand it to a list of characters.
So, looking at the ranges in your examples
1-9 (1 to 9) in [1-9]|10 (equivalent to [123456789]|10)
1-1 (1 to 1) in [1-10] (equivalent to [10] which is the same as [01])
1-( (1 to opening parenthesis) in [1-(10)]
I actually get an error with this in Perl because the range 1 to ( doesn't really make sense.
It is about the character matching. When you say [1-9] it means it matches any individual characters from 1 to 9. Number 10 would be treated as 2 separate characters.
The [] indicates a single character match
for example [ab] would match either a or b
so [1-9] which is effectively shorthand for [123456789] would match a single character that is one of the digits from 1 to 9
Your example of [1-10] would expand the 1-1 to mean all characters in the range 1 to 1 (i.e 1) so the actual regex would expand to be [10] (i.e. either the character 1 or the character 0)
That's the basic definition of a character class. [1-10] means "match any character in the range 1 though 1, or 0". Character classes are evaluated character by character (except for escape sequences and -); they don't understand numbers.
That is because the [] symbols represent character set, e.g. [0-5] matchers 0-5. However, 10 has two digits and therefore [0-9] will not produce an exact match (will only match the first digit, '1' of '10'.
The pipe symbol | can be seen as a "or" operator.
\[([1-9][0-9]|[0-9])\]
This will remove Wikipedia's references when you copy something for your project.

Limit length of characters in a regular expression

Is there a way to limit a regular expression to 100 characters with a regular expression?
\[size=(.*?)\](.*?)\[\/size]
So Look at me! wouldn't work.
I want to limit the numbers, only allow numbers between 1 and 100.
Is there a way to limit a regex to 100 characters WITH regex?
Your example suggests that you'd like to grab a number from inside the regex and then use this number to place a maximum length on another part that is matched later in the regex. This usually isn't possible in a single pass. Your best bet is to have two separate regular expressions:
one to match the maximum length you'd like to use
one which uses the previously extracted value to verify that its own match does not exceed the specified length
If you just want to limit the number of characters matched by an expression, most regular expressions support bounds by using braces. For instance,
\d{3}-\d{3}-\d{4}
will match (US) phone numbers: exactly three digits, then a hyphen, then exactly three digits, then another hyphen, then exactly four digits.
Likewise, you can set upper or lower limits:
\d{5,10}
means "at least 5, but not more than 10 digits".
Update: The OP clarified that he's trying to limit the value, not the length. My new answer is don't use regular expressions for that. Extract the value, then compare it against the maximum you extracted from the size parameter. It's much less error-prone.
If you want to restrict valid input to integer values between 1 and 100, this will do it:
^([1-9]|[1-9][0-9]|100)$
Explanation:
^ = start of input
() = multiple options to match
First argument [1-9] - matches any entries between 1 and 9
| = OR argument separator
Second Argument [1-9][0-9] - matches entries between 10 and 99
Last Argument 100 - Self explanatory - matches entries of 100
This will not accept:
Zero - 0
Any integer preceded with a zero - 01, 021, 001
Any integer greater than 100
If you want numbers from 1 up to 100:
100|[1-9]\d?
Limit the length of characters in a regular expression:
^[a-z]{6,15}$'
Limit length of characters or numbers in a regular expression:
^[a-z | 0-9]{6,15}$'
(^(\d{2})|^(\d{4})|^(\d{5}))$
This expression takes the number of length 2,4 and 5. Valid Inputs are
12
1234
12345
You could do a negative lookahead for the number of characters you want. So if you have a complex regex to get a specific format and you wanted to limit it to say, 50 characters. Then you could preface it with:
(?!.{51})