tweak regex to accept several of the pattern - regex

I validate my input if it passes both regex above.
How can I tweak both regex so that it accepts a list ie (like on input2 and input3). Right now my regex only work on input1.
2 or higher:
^\d{2}\d*$
non 0:
^[1-9]\d*$
input1: 123
input2: 123, 456
input3: 123, 456, 789

First of all, the pattern you posted is equivalent to ^\d{2,}$, which requires the number to have two or more digits. The regex for an integer greater than or equal to 2 is more like ^[+-]?0*([2-9]|[1-9]\d+)$. From your description, it's not clear which of these you intended.
Either way, what you want to use is something like this:
^(<pattern>(,\s|$))+$`
So for your scenario, it would be something like:
^(\d{2,}(,\s|$))+$ #2 or more digits
^(0*([2-9]|[1-9]\d+)(,\s|$))+$ #positive integers >= 2
^(0*[1-9]\d*)(,\s|$))+$ #positive integers > 0
I'm not sure what flavor of regex you're using, but if your engine balks at the redundant use of $ in the patterns above, you could try something like
^(<pattern>,\s)*<pattern>$
instead. Example:
^(\d{2,},\s)*\d{,2}$ #2 or more digits, simplified
Bear in mind that a better way to do this is usually to split the string on the comma + whitespace separator, which will give you an array of strings you can try to parse as integers.

First of all, here's what I'd recommend for your base regexes:
Two or more digits:
\d{2,}
One or more digits:
\d+
Now if you want either of them to match a comma-and-space separated list, you could use:
(?:\d{2,}(?:\s*,\s*)?)+
and
(?:\d+(?:\s*,\s*)?)+
respectively

Try this:
^[1-9]\d*(?:\s*,\s*[1-9]\d*)*$
[1-9]\d* matches one or more digits, the first of which cannot be zero. If there are any more characters after the first number, they must comprise a comma optionally surrounded by whitespace -- \s*,\s* -- followed by another number. And that repeats as many times as necessary.
You can be more strict about the format if you like. For example, if the comma must follow immediately after the number and there must be exactly one space after the comma, you can use this:
^[1-9]\d*(?:, [1-9]\d*)*$

Related

Regex Match string having exact number of a char

I Have some strings:
1:2:3:4:5
2:3:4:5
5:3:2
6:7:8:9:0
How to find strings that have exact numbers of colons?
Example I need to find strings where 4 colons.
Result:
1:2:3:4:5 and 6:7:8:9:0
Edit:
No matter what text between colons, it may so:
qwe:::qwe:
:998:qwe:3ee3:00
I have to specify a number of colons, but using regexp_matches.
It something like filter to search broken strings.
Thanks.
With N being the number you search for:
"^([^:]*:){N}[^:]*$"
Here is a test:
for s in ":::foo:bar" "foo:bar:::" "fo:o:ba::r" ; do echo "$s" | egrep "^([^:]*:){4}[^:]*$" ; done
Change 4 to 3 and 5, to see it not matching.
Maybe postgresql needs specific flags or masking for some elements.
"^([^:]*:){N}[^:]*$"
"^ $"
# matching the whole String/Line, not just a part
"([^:]*:){N}[^:]*"
"( ){N}[^:]*"
# N repetitions of something, followed by an arbitrary number of non-colons (maybe zero)
"([^:]*:)"
# non-colons in arbitrary number (including zero), followed by a colon
You want to use the quantifier syntax {4}. The quantifier is used to indicate that the preceding capture group needs to occur n number of times in order to meet the matching criteria. To find the pattern of five digits separated by semi-colons. something like the following would work.
((\d\:){4}\d)
I am assuming you may want any digit or word character but not whitespace or punctuation. In that case use the word character (\w).
((\w)[\:]){4}(\w))
But depending on what you would like to do with that pattern you may need a different regular expression. If you wanted to capture and replace all the colons while leaving the digits intact your pattern would need to use string replacement or more advanced grouping.
Any number of any characters, including 4 :
((.*:){4}.*)

How to extract group of numbers from a phrase using regex?

I have multiple sentences which look like the following sentence -
069054 my name is black fox, $1234. phone number:1234567
I need to extract to extract the first word (or numbers, in this example its 069054).
The conditions that needs to be met are:
it should only consist of 6 digits.
It should be the first thing in the sentence.
If it has more or less digits, i should ignore it.
should only consist of numbers, no chars allowed
Here is what i have, but its not working out for me.
^([\d]{6})$
This is the regex you are looking for:
^(\d{6})(?!\d)
Just remove the $ from the end and replace it with (?!\d). It means the six digits which are not followed by any digit.
If you wish to avoid picking digits from input like 123456xyz then use this one:
^(\d{6})(?![\da-zA-Z])

Capturing exact amount of numbers without Whitespaces

I have a series of 8-digit-numbers that I need to capture via RegEx.
Single whitespaces can occur before, after and in some cases between the digits. In some cases, other chars follow. Here's the most common variations, each of which I want to capture as 12345678:
123456789
12345678
1234567 89S
12345 678 9
123 456789
123456 789
Is this possible?
I think a regex like:
(( )?\d){8}
Would suffice to capture the digits - I'd then remove the whitespace (before further processing) as a separate step.
I'm not sure how strictly to interpret the OP's "single whitespaces" requirement, but it's why I've structured my RegEx to accept 8 digits, each of which is optionally prefixed by a single space character.
If it should only match if there are single spaces, and not any more, the above works whereas the "strip whitespace first" or "strip non-digits first" approaches will not.
If more spaces are allowed, it's easy to change the ? to a * or any fixed upper limit.
This is not possible in a single "regex" step. I can go into more detail if you like, but basically regex cannot "count" (it can only match a specified match size, such as "8 numbers", but not "an unknown number of characters, 8 of which are numbers").
You need to do this in two stages -
first remove whitespace.
then perform a regex match.
For instance, in ruby:
thingtomatch = " 12 3456 7899X"
temp = thingtomatch.squeeze(' ').strip # => temp="1234567899X"
matched_digits = temp.match(/(\d{8}).*/)[1]
(Or, as other answers have suggested, you could perform a regex match and then remove whitespace from the result.)
You can do it, but in two steps:
First, remove non-digits:
s/[^\d]//g
Second, match your digits:
m/^(\d{8})$/

Regex to check for 4 consecutive numbers

Can I use
\d\d\d\d[^\d]
to check for four consecutive numbers?
For example,
411112 OK
455553 OK
1200003 OK
f44443 OK
g55553 OK
3333 OK
f4442 No
45553 No
f4444g4444 No
f44444444 No
If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.
Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.
Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.
The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.
This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.
Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.
That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.

How to detect exact length in regex

I have two regular expressions that validate the values entered.
One that allows any length of Alpha-Numeric value:
#"^\s*(?<ALPHA>[A-Z0-9]+)\s*"
And the other only allows numerical values:
#"^\s*(?<NUM>[0-9]{10})"
How can I get a numerical string of the length of 11 not to be catched by the NUM regex.
I think what you're trying to say is that you don't want to allow any more than 10 digits. So, just add a $ at the end to specify the end of the regex.
Example: #"^\s*(?[0-9]{10})$"
Here's my original answer, but I think I read you too exact.
string myRegexString = `#"(?!(^\d{11}$)` ... your regex here ... )";
That reads "while ahead is not, start, 11 digits, end"
If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...
^\s*([0-9]{10})\z
That will accept 1234567890 but reject 12345678901.
Do you mean you want to match up to 10 digits? Try this:
#"^\s*[0-9]{1,10}\s*$"
If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:
^\s*(?:[0-9]{10})\s*$
That will match any number that is exactly 10 digits long (with optional space on either side).
var pattern =/\b[0-9]{10}$\b/;
// the b modifier is used for boundary and $ is used for exact length
Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.
This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.
Non-capturing version: (only matches, the matched digits are not stored)
^\s*(?:\d{10})\s*$
Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)
^\s*(\d{10})\s*$
You could try alternation?
^\s*(?\d{1,10}|\d{12,})