1

I wanted to know if my re.search string, which is repetitive, could be cleaned up. The code: re.search(r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"

As you can see, the line ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) is being used 4 times, and I feel like it could be shortened by assigning it to a variable? But I have no clue on how to use variables in this string.

If you could, please do explain the code as well, which would be greatly appreciated! Thanks in advance!

Skippy
  • 23
  • 3
  • what did you want to get? show an output so we can make a useful regex for you. – Mehmaam Aug 12 '22 at 04:59
  • I actually have a bit of other code. The only thing I want is to replace the string and make it cleaner, nothing else. But since you asked, it asks the user for an IPv4 Address (e.g: 127.36.24.35) and then "validates" it (makes sure the numbers are in the range of 0 and 255, inclusive). Then it returns True or False. – Skippy Aug 12 '22 at 05:43
  • ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ Try this. – Mehmaam Aug 12 '22 at 05:57
  • 1
    If you wouldn't mind posting it as a separate answer, and explain it, It would be appreciated and I can also mark it as the correct answer if it works! – Skippy Aug 12 '22 at 06:17
  • Also, I feel the code works like this: `((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}` checks for any number between 0 and 255, followed by a period 3 times, and `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)` just checks for a number between 0 and 255. And I believe the `[01]?[0-9][0-9]?` checks for either 000 - 099 or 100 - 199 optionally? – Skippy Aug 12 '22 at 06:24

1 Answers1

1
((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

\b --> Matches, without consuming any characters, immediately between a character matched by \w and a character not matched by \w (in either order). It cannot be used to separate non-words from words.

| --> Equivalent to Boolean OR

? --> matches a previous token between zero and one time, as many times as possible giving back as needed.

{3} --> match previous regex 3 times

$--> Matches the end of a string without consuming any characters. If multiline mode is used, this will also match immediately before a newline character.

 ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

\b character boundary
(\.|$) after the end of 3 digits, it can end the string and place a dot 
{3} repeat the previous string 3 times then move on to the last one

$ matches last character.
Mehmaam
  • 573
  • 7
  • 22
  • I can try my best but if you face any issues then tell me. I can take a reference from here https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp but the answer with verified tick also has an issue, it can accept this value too. 10.10.10.10. also verify the last dot so i can make a new one hope it will help you. – Mehmaam Aug 12 '22 at 07:05