What does this regular expression mean? - regex

could somebody please tell me what the following expressions means:
\d{6,8}
As far as I know it's a regular exp

Between 6 and 8 numeric digits.
(As it's not anchored to boundaries or start & end of string, it would also match between 6 and 8 digits within a longer series of digits - for instance, it will match 123456, 1234567, 1234678, but also the first 8 digits of 123456789.)
\d is a character class - it could also have been written as [0-9]. The {} part is a repetition count; it could be a single number, e.g. {6}, or, as in this case, a range - so the {6,8} means "the previous thing, repeated between 6 and 8 times".

it matches between 6 and 8 sequential numeric digits.
\d is equivalent to the character class [0-9], and the {,} notation specifies an exact number of times that a pattern has to match.

matches a digit that is of length between 6 and 8

it means, at least 6 digits and no more than 8 digits

It means between 6 to 8 numbers in a row.
\d means a number [0-9]
{6, 8} means min. of 6, max. of 8
You use curly braces to describe how many of the previous character you want to look for. Entering a single number, like {3} means 3 in a row. Adding a second number changes this into min/max.
http://www.regular-expressions.info
is the best site on the web for learning about regular expressions.

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]*.

RegExp Quantifier to Match Pattern Exactly n or m Times Instead of n to m Times

I want to match 2 and 4 digit numbers. This RegExp is a obvious choice:
/[0-9]{2,4}/
However, this matches 3 Digit numbers. Is there a way around this in regexp?
You can use ([0-9]{2}){1,2}:
/\b([0-9]{2}){1,2}\b/
Above regular expression is not general; it was possible because 4 = 2 * 2.
More general solution is:
/\b[0-9]{2}\b|\b[0-9]{4}\b/
NOTE: \b (word boundary) was used to prevent matching 2 digits from 3 digits (or 5, 6, ... digit string).
You can use negative lookbehind/lookahead to accomplish this:
Here's one possible way to accomplish what you're trying to do.
/(?<!\d)(\d{2}|\d{4})(?!\d)/
This says - find a 2 or 4 digit number that is not preceded or followed by another number. This differs from the answer above in that it will match ALL 2 and 4 digit numbers including those that are not surrounded by spaces such as the "12" in the string "abc12def".
Which way you choose will depend on what in particular you are looking for.

How to recreate this reg expression to fit more conditions?

Regular expression:
/(\b|[x])\d{7}\b/
Essentially what I'm trying to do is only highlight the digits and not the letter "x". I also would like to highlight the 7 digits even if it has multiple spacse between the main number and the 7 digits. I seem to be only be able to highlight expressions when its followed by exactly 1 space.
Thanks!
So you want to highlight exactly 7 digits that are followed by space or x?
Use this pattern.
(?<=[x ])\d{7}(?!\d)
(?<=[x ]) is lookbehind. means only match 7 digits when the preceding character is x or whitespace.
(?!\d). is negated lookahead. should be exact 7 digits. so the next character after 7 digits should not be digit.

regular expression on double(10,2)

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.