Regular Expression- have different digits - regex

How I use regular expression in order to check if my number have different digits.
For example: the number: 5554 is ok but the number: 5555 is not ok.

This regex checks if all the digits are the same. Then take the opposite and you will find if your number has different digits.
^(\d)\1*$
This regex checks if at least one digit is different:
(\d)((?!\1)\d)+
EDIT in C# with RegularExpression Attribute
[RegularExpression(#"(\d)((?!\1)\d)+")]

In perl for any digits:
/^(\d)\1*$/
Some tests:
Content of script.pl:
use warnings;
use strict;
while ( <DATA> ) {
print unless /^(\d)\1*$/;
}
__DATA__
55555
56
5556
56565
5
6555
55655
55
Running the script in a linux machine:
perl script.pl
Result:
56
5556
56565
6555
55655

Don't use regular expressions for that.

Four digits which are not the same:
([0-9])(?!\1\1\1)[0-9]{3}
Backreferences in a negative lookahead don't work in some regex implementations. I tried this regex with grep -P.

var a = 5555;
var b = 5554;
var a1 = a.ToString().ToCharArray().Distinct().Count();
var b1 = b.ToString().ToCharArray().Distinct().Count();
And then check for count.

Related

Regex for valid SSN or other ID

I'm a regex newbie and I've got a valid regex for SSNs:
/^(\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}]*$/
But I now need to expand it to accept either an SSN or another alphanumeric ID of 7 characters, like this:
/^[a-zA-Z0-9]{7}$/
I thought it'd be as simple as grouping the SSN and adding an OR | but my tests are still failing. This is what I've got now:
/^((\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}])|[a-zA-Z0-9]{7}$/
What am I doing wrong? And is there a more elegant way to say either SSN or my other ID?
Thanks for any helpful tips.
Valid SSNs:
123-45-6789
123456789
123 45 6789
Valid ID: aCe8999
I have modified your first regex also a bit, below is demo program. This is as per my understanding of the problem. Let me know if any modification is needed.
my #ids = (
'123-45-6789',
'123456789',
'123 45 6789',
'1234567893434', # invalid
'123456789wwsd', # invalid
'aCe8999',
'aCe8999asa' # invalid
);
for (#ids) {
say "match = $&" if $_ =~ /^ (?:\d{3} ([ \-])? \d{2} \1? \d{4})$ | ^[a-zA-Z0-9]{7}$/x ;
}
Output:
match = 123-45-6789
match = 123456789
match = 123 45 6789
match = aCe8999
Your first regex got some problems. The important thing about it is that it accepts {{{{}}}}} which means you have built a wrong character class. Also it matches 123-45 6789 (notice the mixture of space and dash).
To mean OR in regular expressions you need to use pipe | and remember that each symbol belongs to the side that it resides. So for example ^1|2$ checks for strings beginning with 1 or ending with 2 not only two individual input strings 1 and 2.
To apply the exact match you need to do ^1$|^2$ or ^(1|2)$.
With the second regex ^[a-zA-Z0-9]{7}$ you are not saying alphanumeric ID of 7 characters but you are saying numeric, alphabetic or alphanumeric. So it matches 1234567 too. If this is not a problem, the following regex is the solution by eliminating the said issues:
^\d{3}([ -]?)\d\d\1\d{4}$|^[a-zA-Z0-9]{7}$

How can I identify a number of variable length with a regex?

I need a Perl regex to pull a number of between six and ten digits out of a string. The number will always follow a particular word followed by a space (case unknown).
For example, if the word I was looking for is 'string':
some random text blah blah blahSTRING 1234567890some more random text
Desired output:
1234567890
Another example:
yet more random textra ra rastring 654321hey hey my my
Desired output:
654321
I want to load the result into a variable.
/string ([0-9]{6,10})/i
string matches STRING and string as the expression ends with i (case insenstive matching)
matches a space
(starts a capture group to capture the number you trying to get
[0-9]{6,10}matches a number with 6 to 10 places
https://regex101.com/r/mB1zF4/1
Group 1 should contain your number with
/^.*string (\d+).*$/i
Thanks everyone, between all the responses and a bit of googling I ended up with
#!/usr/local/bin/perl -w
use strict;
my $string = 'sgtusadl;fdsas;adlhstring 12345678daf;slkdfja;dflk';
my ( $number ) = $string =~ m/string\s\d{6,10}/gi;
$number =~ s/[^0-9]//g;
print "number is $number\n";
exit 0;

Remove empty spaces and period

I cannot get this regex to work:
"4. 182 ex" (number, period, 2 blank spaces, 3 numbers, blank space, 2 characters"
The regex syntax should return "4182" and remove period, blank spaces, and characters.
Can you help me please?
EDIT!!!
Thanks everyone but I missed the key question:
a) the regex shall only find the value (4182) when the same line contains a specific text for example "magic", so for example:
"Magic 4. 182 ex"
b) the regex shall "only" find the value (4182) when the table contains a specific text for example "Magic":
"Magic 4. 182 ex
Lisefeo 2. 123 fg
Nioos 3. 124 df"
specific text = exact match or contains those charachters
My regex that I've tried so far but does it work for a whole table (not just a line) ?
(Magic.*?(\d).\s\s(\d{3})\s\w\w)
Just remove all characters that are not digit:
Perl:
$string =~ s/\D+//g;
or
php:
$string = preg_replace('/\D+/', '', $string);
According to your updated question, you could do:
$string =~ s/^Magic(\d+)\. (\d{3})\b.*$/$1$2/
or, with php:
$string = preg_replace('/^Magic(\d+)\. (\d{3})\b.*$/', '$1$2', $string);
For it to match exactly what you said, use:
(\d)\.\s\s(\d{3})\s\w\w
You'll get it in two groups, first digit and second digit group.
RegEx101 exmple
Regards.
^([\d]+)\.[\s]+([\d]+)[\s]..
Tested with perl:
> echo "4. 182 ex" | perl -lne 'print $1,$2 if(/^([\d]+)\.[\s]+([\d]+)[\s]../)'
4182

Find numbers in a sentence by regex

I need a regular expression that will find all the numbers on a sentence.
For example:
"I have 3 bananas and 37 balloons"
I will get:
3
37
"The time is 20:00 and I have 7 tanks"
I will get:
20
00
7
Split your string by [^0-9]+.
JAVA: String[] numbers = "yourString".split("[^0-9]+");
JavaScript: var numbers = "yourString".split(/[^0-9]+/);
PHP: $numbers = preg_split("/[^0-9]+/", "yourString");
The regex itself is as simple as \d+, but you will also need to set a flag to match it globally, the syntax of which depends on the programming language or software you are using.
EDIT: Some examples:
Python:
import re
re.findall(r"\d+", my_string)
JavaScript:
myString.match(/\d+/g)
The regex you are looking for is [0-9]+ or \d+. You should then get multiple matches for the sentence.

Adding an AND clause to a regex

I have this simple regex,
[\d]{1,5}
that matches any integer between 0 and 99999.
How would I modify it so that it didn't match 0, but matches 01 and 10, etc?
I know there is a way to do an OR like so...
[\d]{1,5}|[^0]{1}
(doesn't make much sense)
There a way to do an AND?
probably better off with something like:
0*[1-9]+[\d]{0,4}
If I'm right that translates to "zero or more zeros followed by at least one of the characters included in '1-9' and then up to 4 trailing decimal characters"
Mike
I think the simplest way would be:
[1-9]\d{0,4}
throw that between a ^$ if it makes sense in your case, and if so, add a 0* to the beginning:
^0*[1-9]\d{0,4}$
My vote is to keep the regex simple and do that as a separate compare outside the regex. If the regex passes, convert it to an int and make sure the converted value is > 0.
But I know that sometimes one regex in a config file or validation property on a control is all you get.
How about an OR between single digit numbers you will accept and multiple-digit numbers:
^[1-9]$|^\d{2,5}$
I think a negative lookahead would work. Try this:
#!/bin/perl -w
while (<>)
{
chomp;
print "OK: $_\n" if m/^(?!0+$)\d{1,6}$/;
}
Example trace:
0
00
000
0000
00000
000000
0000001
000001
OK: 000001
101
OK: 101
01
OK: 01
00001
OK: 00001
1000
OK: 1000
101
OK: 101
By using look-aheads you can achieve the effect of AND.
^(?=regex1)(?=regex2)(?=regex3).*
Though there is a bug in Internet Explorer, that sometimes doesn't treat (?= ) as zero-width.
http://blog.stevenlevithan.com/archives/regex-lookahead-bug
In your case:
^(?=\d{1,5}$)(?=.*?[1-9]).*
It looks like you are searching for 2 different conditions. Why not break it out to 2 expressions? It might be simpler and more readable.
var str = user_string;
if ('0' != str && str.matches(/^\d{1,5}$/) {
// code for match
}
or the following if a string of 0's is not valid as well
var str = user_string;
if (!str.matches(/^0+$/) && str.matches(/^\d{1,5}$/) {
// code for match
}
Just because you can do it all in one regex doesn't mean that you should.
^([1-9][0-9]{0,4}|[0-9]{,1}[1-9][0-9]{,3}|[0-9]{,2}[1-9][0-9]{,2}|[0-9]{,3}[1-9][0-9]|[0-9]{,4}[1-9])$
Not pretty, but it should work. This is more of a brute force approach. There's a better way to do it via grouping as well, but I'm drawing a blank on the actual implementation at the moment.