Need help for a regex - regex

I'd like to build a regex, but I'm stuck.
This is the format I'm looking for:
x;y => 7 times, separated by -
where x is a number from 1 to 7
and y is a number from 1 to 4
Here's what I've done so far:
^([0-7;&-]*)$
example :
1;1-2;3-3;1-4;4-5;2-6;2-7;4
Could you help me?
Thank you

Your current pattern is a broad match as repeating your character class does not take any structure into account or different ranges for the digits.
You could match a digit 1-7, then : and a digit 1-4. Then repeat 6 times the same pattern preceded with a hyphen.
^[1-7];[1-4](?:-[1-7];[1-4]){6}$
Regex demo

Related

How to match the 2 last numbers of an IP adress?

i don't know what i'm missing ^^
I'm on bash script, i tested it with https://regex101.com/
Here is some ip adress:
192.168.23.84
192.168.112.34
192.168.43.227
And i want to match only the numbers that are not by group of 3, i've tried:
\.([0-9]{2})\.|\.([0-9]{1})\.|\.([0-9]{2})$|\.([0-9]{1})$
I don't know why i'm not matching the two last numbers, Is there a better solution ?
^(\d{1,2})\.|\.(\d{1,2})\.|\.(\d{1,2})$
You've got 3 cases : 1|2 digits after the start followed by a dot, 1|2 digits betweens dots, 1|2 digit after a dot at the end of the line.
With this solution you'll have 3 groups and you won't be able to known which part for the middle group has not 3 digits. If you want 4 groups use this one :
(?:^(\d{1,2})\.)|(?:^\d{3}\.(\d{1,2})\.)|(?:\.(\d{2})$)|(?:\.(\d{2})\.\d{2,3})
Edit : https://regex101.com/r/eAc8XX/1
Be aware that I'm not looking for a valid IPv4 with theses regex.

find and replace 15/16 digit numbers

I can see partial answers to my question but nothing that entirely answers my problem.
I'm looking for some script to run that will find and replace specifically only 15 and 16 digit numbers in a file.
rather than replace it with any one specific thing uniformly I want to retain the first 10 digits of the number and replace the last 6 with six 'X's
For example:
1234567890123456 would become: 1234567890XXXXXX
Help greatly appreciated.
P.S= The same question is raised here but only the question given in the subject title is addressed and not the detail of the text (the chap wanted not only to find 15 and 16 digit numbers... but wanted to replace the last digits with 'X')
PHP Find a 15 or 16-digit number in a long string
I guess you're using php.
With preg_replace you could do:
$str = 1234567890123456;
$res = preg_replace('/\b(\d{10})\d{5,6}\d/', '$1XXXXXX', $res);
Explanation:
/ : regex delimiter
\b : word boundary, make sure we don't have digits before
( : begin capture group
\d{10} : 10 digits
) : end capture group
\d{5,6} : 5 or 6 digits
\b : word boundary, make sure we don't have digits before
/ : regex delimiter

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.

Creating a regex that allows 9 or 10 digits

I need help with a regex
I need it to match either a 9 or 10 digit value that starts with 50.
I have:
^[ ]*(50)[0-9]{7}[ ]*$
which allows 9 digits.
How can I expand this so that it also allows 10 digits?
Add the range {7,8}
^[ ]*(50)[0-9]{7,8}[ ]*$
FYI this site describes the standard quantifiers that you can use in a regular expression:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
Try with following regex:
^[ ]*50\d{7,8}[ ]*$
This regex will match what you need:
^\s*50\d{7,8}\s*$
This will match all 9 or 10 digit numbers starting with 50 with an unlimited number of spaces before, or after, them on the line.
If you want to match all 9 or 10 digit numbers starting with 50 regardless of position and number of spaces etc then:
50\d{7,8}
will do exactly what you need.
Here's what you need: (50)\d{7,8}

Require RegEx Engine to fail

I need a regex that matches the following format.
hhhh:mm or hhhhmm
hhhh are hours (first digit should match 1-9 followed by numbers, not more than 4 digits)
mm are minutes (first digit should match 0-5 followed by a number, not more than 2 digits)
The following format should also be possible - mm or m
So far I have
^([1-9]\d{0,3}:?)?([0-5]\d{0,2})?\d
which matches what i want but doesn't fail if I enter e.g. 4444444.
In fact if I have less than 3 digits they should be treated a minutes in the range 0..59.
Any help is appreciated.
I'm not a regex guru at all.
You probably want something like:
^(?:([1-9]\d{0,3}):?)?([0-5]?\d)$
That would allow a single digit minute even if hours part is present. If you don't want that it could be solved in some different ways, eg:
^(?:([1-9]\d{0,3}):?)?([0-5]\d)$|^(\d)$