Regex to match total number of interrupted characters - regex

I need to be able to detect when a string has 8 or more numbers, even when they are separated by periods - but that total 8 or more cannot include the periods. In other words, I can ignore periods when looking for a numeral string that includes 8 or more numbers.
I've tried countless combinations of capturing groups and non-capturing groups, regular sets and negated sets, and I just can't figure it out. But I've simplified my examples, below, to show the issue.
For example, the following regex will match, even though there are only 6 numbers total, but there are 8 total characters (obviously):
Expression: [0-9\.]{8,}
Text: 12.34.56
Is there a regex expression that would allow me to ignore those periods?

This will validate a digit 8 or more times ^\D*(?:\d\D*){8,}$
Basically, the single digit \d is surrounded by optional non-digits \D,
and matched in a group, 8 or more times.
Formatted:
^ # Beginning of string
\D* # 0 to many non-digits
(?: # Cluster group
\d # single digit
\D* # 0 to many non-digits
){8,} # 8 or more times
$ # End of string

Related

Regex (Has between 1 and 4 digits and as many characters as possible)

Hi trying to create a regex that ensures you have between 1 and 4 number of digits and also as many characters as possible
Here's what I have written so far ^([A-Za-z]+([0-9]){1,4}$)
This doesnt allow me to have characters after the digits
You might repeat the whole part 1-4 times and match optional trailing chars a-z
^(?:[A-Za-z]*[0-9]){1,4}[A-Za-z]*$
The pattern matches
^ Start of string
(?: Non capture group
[A-Za-z]*[0-9]){1,4} Repeat matching 1-4 times optionalchars a-z and a single digit
[A-Za-z]* Optionally repeat char A-Za-z
$ End of string
Regex demo

Regex to match string of limited length and if number digit are present it can't be more than 5 digit

im looking for regex that can match string with requirement below.
must be 5 to 15 characters
Alphanumeric, can accept fully alphabet, if numeric are present, it must not exceed 5 digit and it can be in anywhere in the string.
Example accepted input
helloworld
123helloworld56
1h2e3l4l5oworld
12345
if the numeric digit exceeded 5 it shall be rejected. Example rejected input:
123456
123hello4567
So far i have tried while looking online and done some tweaking, but none work as expected.
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
^(?=.*\d){0,5}.{0,15}$
I have stuck on this for some time now, any help are appreciated!
If there can not be more than 5 digits in total, that means you should not be able to match 6 digits.
You can use a negative lookahead to assert what is on the right can not match 6 digits.
^(?!(?:[^\d\r\n]*\d){6})[a-zA-Z0-9]{5,15}$
Explanation
^ Start of string
(?! Negative lookahead, assert what is at the right is not
(?:[^\d\r\n]*\d){6} Match 6 times any char except a newline or a digit, then match a digit
) Close lookahead
[a-zA-Z0-9]{5,15} Match 5-15 times any of the listed in the character class
$ End of string
Regex demo
Note that using [1-9] in a character class does not match the 0, and \d will
About the patterns in the question
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
Here, the lookahead will always be true as all the parts in it are optional. It could also match an empty string as the quantifier {0,15} starts at 0, which makes it optional.
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
The pattern asserts a string with 5-15 times any of the listed in the character class. But the matching starts with 1-15 times a char a-zA-Z followed by matching 0-5 times a digit at the end of the string.
^(?=.*\d){0,5}.{0,15}$
The pattern optionally asserts 0-5 digits which is always true as it is optional. Then it matches 0-15 times any char.

RegEx no whitespace in third column of mobile number

I came up with following regex for a mobile phone number:
^\+([0-9]{1,} )+([0-9]{2,} )+[0-9]+
Example of valid number:
+385 552 8221520
What would be the according regex, so I don't get a match if any whitespace in the third capturing group is found:
+385 552 82215 20 (gives a match now, but not fine!)
Your pattern contains quantified groupings. The ^\+([0-9]{1,} )+([0-9]{2,} )+[0-9]+ pattern matches a string that starts with +, then contains 1 or more repetitons of 1+ digits followed with a space, then 1+ repetitions of 2+ digits followed with a space and then 1+ digits. Thus, it matches many space separated digit chunks. Also, it is not anchored at the end of the string with $ and might match strings that contain rubbish at the end if used with a regex method that allows partial matches.
To limit to just three space separated digit chunks, you may use any of the following:
^\+[0-9]+ +[0-9]{2,} +[0-9]+$ - if there can be 1 or more spaces between the digit groups
^\+[0-9]+ ?[0-9]{2,} ?[0-9]+$ - if there can be 1 or 0 spaces between digit groups
^\+[0-9]+ *[0-9]{2,} *[0-9]+$ - if there can be 0 or more spaces between digit groups.
Note that $ added at the end of each pattern. Also, see this regex demo.

perl regex - catching four numbers separated by space

I know that we can use regex in perl to catch numbers using [\d], but my pattern is like this:
261 193 546 302
or it could be like this:
16 0 98 120
The point is - I just want to catch a line that has any four numbers separated by a space. Each number can be made up of any number of digits, it could be a single-digit number, or a double-digit number, and so on.
^\d+(?:\s+\d+){3}$
Try this.This should do it for you.
You don't explicitly have to wrap the token inside of a character class. And for this you want to assert the start of the string and end of the string positions, so I would use anchors and quantify a non-capturing group "3" times.
^\d+(?: \d+){3}$
Explanation:
^ # the beginning of the string
\d+ # digits (0-9) (1 or more times)
(?: # group, but do not capture (3 times):
# ' '
\d+ # digits (0-9) (1 or more times)
){3} # end of grouping
$ # before an optional \n, and the end of the string
Based on your requirements to "catch a line that has any four numbers separated by a space". I would use the following as it contains a capture group which will contain your number sequence and will ignore any leading or tailing spaces.
((?:\d+\s){3}\d+)
REGEX101
Usage in Perl
$re = "/((?:\\d+\\s){3}\\d+)/";
As you can see it will match exactly 4 numbers separated by a single space and will ignore preceding and trailing characters.
Alternate
If you where being explicit and actually want to capture the whole line including any other characters this will be better suited.
(^.*(?:\d+\s){3}\d+.*$)
REGEX101
Usage In Perl
$re = "/(^.*(?:\\d+\\s){3}\\d+.*$)/mx";
Note this will match numbers with decimal places due to the way it is structured.
Try ^\d+\s\d+\s\d+\s\d+$. That will match 4 numbers with spaces and nothing else.
Sample

Regular Expression, with number spaces dashes limited to 8-13 numbers

I am trying to do a regular expression to validate a number between 9 and 13 numbers, but the sequence can have dashes and spaces and the ideal is to not have more than one space or dash consecutively.
this rule allow me to control the validation between 9 and 13
/^[\d]{9,13}$/
now to add dashes and spaces
/^[\d -]{9,13}$/
I think I need something like that, but I need to count the numbers
/^[ -](?:\d){9,13}$/
Any tips?
Notice how my regex starts and ends with a digit. Also, this prevents consecutive spaces and dashes.
/^\d([ \-]?\d){7,12}$/
It appears that you don't want leading or trailing spaces and dashes. This should do it.
/^\d([- ]*\d){8,12}$/
Regular expression:
\d digits (0-9)
( group and capture to \1 (between 8 and 12 times)
[- ]* any character of: '-', ' ' (0 or more times)
\d digits (0-9)
){8,12} end of \1
Another option: A digit followed any number of space or dash 8-12 times, followed by a digit.
/^(\d[- ]*){8,12}\d$/
Use look aheads to assert the various constraints:
/^(?!.*( |--))(?=(\D*\d){9,13}\D*$)[\d -]+$/
Assuming a dash following a space or vice versa is ok:
^( -?|- ?)?(\d( -?|- ?)?){9,13}$
Explanation:
( -?|- ?) - this is equivalent to ( | -|-|- ). Note that there can't be 2 consecutive dashes or spaces here, and this can only appear at the start or directly after a digit, so this prevents 2 consecutive dashes or spaces in the string.
And there clearly must be exactly one digit in (\d( -?|- ?)?), thus the {9,13} enforces 9-13 digits.
Assuming a dash following a space or vice versa is NOT ok:
^[ -]?(\d[ -]?){9,13}$
Explanation similar to the above.
Both of the above allows the string to start or end with a digit, dash or space.