Powershell Regular Expression - regex

In Powershell, how do I specify a regular expression consisting of one, two or three digits, followed by a decimal point and one or two more digits, for use in a -match comparison?
[0-9][0-9]*[0-9]*\.[0-9][0-9]* is the closest I can come up with, but this allows more digits than I want. I can't find any way to limit a term to a maximum number of repetitions.

You can try use the following: \d{1,3}\.\d{1,2}. The numbers within the braces denote the minimum and maximum amount of repetitions which the regex engine will match. An example is available here.
The issue with your expression is that your are using the *. This means 0 or more repetitions of, thus, [0-9]* would fit an empty string, a string with 1 digit, 2 digits, and so on.
An alternative (and closer to what you did) would be to use the ? operator instead, which means 0 or 1 instances of, thus this: [0-9][0-9]*[0-9]*\.[0-9][0-9]* would become this: [0-9][0-9]?[0-9]?\.[0-9][0-9]?, which will match a digit followed by, optionally at most 2 more numbers, followed by a period, by a digit and optionally 1 more digit.

Related

Regex to allow one special character with at least 5 digits and maximum 6 digits

I have created a regex which follows the following parameters:
Minimum length: 5
Maximum length: 6
Needs to have at least 5 digits
Space and Special characters allowed: #&()_+[]:;',/.\-"*
No alphabets allowed
The regex I created is :
^\d{3}[_\+\[\]\:\;\'\,\/.\-"!##$%^&*()\s]{0,1}\d{2,3}$
This is fulfilling the length requirements and 5 digit requirement, however it is not allowing special characters. I am blocked due to this and unable to find any solution, please help.
You could do it with
^(?:(?=.{6}$)\d*[-#&()_+[\]:;',\/.\\"*]\d*|\d{5,6})$
if your regex-flavor supports look-aheads.
It uses two alternations. The first starts by checking the length, which including a special character always must be 6 (to allow for 5 digits), with a positive look-ahead. Then it matches any number of digits, followed by a special character, and finally any number of digits.
The other alternative just checks for 5-6 digits.
See it here at regex101.

Regex to match specific number format

I am having trouble figuring out how to write a regex to match a number (technically a string) with the following rules:
all numeric
must be exactly 11 digits
it must start with at least 2 zeros
it may not start with more than 4 zeros
I can use \d{11} to match for the exactly 11 digits, and ^0{2,4] to match the leading zeros part, but I can't figure out how to combine them.
^00(?!000)\d{9}$
It checks for two zeroes and then checks that there are not more than 2 0's following it.
if it is not it checks the other 9 numbers to the end of the string.
This assumes your engine supports lookaheads.

I need regex to only take numbers in string

I thought I had it with [0-9] but when I ran it that only took one number.
The string goes for example:
1 note
1,234 notes
68,000 notes
I want it so it takes the whole number and leaves out the notes part and the spaces and also the comma so just the full number.
The [0-9] would only take the first number of the string even when there wasnt a comma.
So how to only take the number please?
[0-9] means any one character between 0 and 9. What you are looking for is these characters repeated any number of times, but no other character should be there. The correct way to write this is [0-9]+.
M+, where M is some regex rule is equivalent to M M*, where * means 0 or more occurrences. So M+ can be inferred as at least one occurrence of portions specified by M.
EDIT: The question now also states that the entire number should be read, but the comma should be excluded from the output. AFAIK, this is impossible to be done using only regex, as the matched text can't be different from the stored text. A possible solution is to add , to the list of allowed characters and parse the result to remove them later on.

how to find integer with comma and zeros after that (regex)?

I try to create regex(es) to extract all integers. It can be 6 -12 bur also +6.000 or -5,0 and onother one to extract real numbers which are not integers, for example 3.14, -6,26 but no 5.0.
For finding integers I tried "^[+-]?([0-9]+)(\\[.,]0{1,})?$" but it doesn't work on -6.00. And I have no idea how to create second regex (how to exclude integers with comas or dots and then zeros). Any help appreciated.
The problem with your integer regex appears to be the backslash(es). I don't know any regex engine in which you would need to escape the opening bracket of a character class, and you certainly don't want to match a literal backslash. Also, to a regex engine that understands it at all, the quantifier {1,} is an uglier, more complex way of saying +.
This should do your integer matching:
"^[+-]?[0-9]+([.,]0+)?$"
And this variation should do your non-integer matching:
"^[+-]?[0-9]+[.,]0*[1-9][0-9]*$"
In both cases I omitted parentheses not needed for expressing a correct pattern, but if you need to capture parts of the match then you will want to add some back in. You might also want to convert the grouping parentheses into non-capturing form if you are using a regex engine that supports it.
Also, the real number pattern requires at least one digit before the fraction separator character, per your examples. It would be easy to convert the pattern to also match strings of the form .1 or -.17. Similarly, the integer pattern requires at least one zero in the fraction part if there is a fraction separator, and restriction could be removed, too.

Regular expression for optional commas with a 6 character limit

I need a regex that allows numbers and optional commas, but the entire length cannot be greater than 6.
^[0-9]+([,]*[0-9]+)*$ allows numbers and optional commas.
^([0-9]+([,]*[0-9]+)*){0,6}$ does not limit the total length to 6.
If your regex engine supports lookahead assertions — most do — then you can write:
^(?=[0-9,]{1,6}$)[0-9]+(,[0-9]+)*$
The (?=[0-9,]{1,6}$) part is a "positive lookahead assertion", and means "looking forward from this point in the string, I see [0-9,]{1,6}$". So, in essence, the above regex is a combination of these two:
^[0-9,]{1,6}$
^[0-9]+(,[0-9]+)*$
and enforces them both.
(That said, it's likely to be clearer if you simply enforce the length restriction as a separate step, rather than incorporating the above into a single regex.)
^([\,0-9]{0,6})$
This regex simply allows any of the characters (comma, zero through nine) zero through six times.
If you require that the input start with a number, use this:
^([0-9]{1}[\,0-9]{0,5})$
Some additional ways -
^(?=.{1,6}$)\d+(?:,?\d)*$
^(?=.{1,6}$)\d(?:[,\d]*\d)?$