Pages: 1
RSS
Trying to work out how to find certain IPv4 addresses in the middle of a string
 

I am trying to create two separate regex expressions, the first is trying to find ipv4 addresses in  the 192.168.0.x range in the middle of a large string, which also contains large amounts of white-spaces and other numerals.
[P]The second expression is underneath the same conditions, but the address is in the 10.x.x range :

Sample valid addresses: 192.168.0.0 / 192.168.0.255 and 10.0.0.0 / 10.0.255.255

I have very little experience with regex: my attempt at solving the first problem was (192\.168\.0\.)+([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])

[P]I believe that this is on the wrong track, and want to see how it's properly done, or at least some tips on finding a solution. I am using a java environment to parse the strings
 
Regular expressions are not specific to The Bat, so don't despair if you don't find (get) the answer here. They are used in countless applications and programming and scripting languages, so, there are many other places on the internet where you may still find an answer.

A generic ipv4 finding expression is shown and explained here: https://regex101.com/r/dA7sJ5/3

Even if it's not precisely what you're asking for (it doesn't filter for those specific ranges), I thought the site and its RegEx debugger might still be helpful.
I volunteer as a moderator to help keep the forum tidy. I do not work for Ritlabs SRL.
 
I modified the code from Daniel's link. This seems to solve the first case you are looking for:

(192.168.0.\d{1,3})
Test it though to make sure.
Edited: Ulrich Leininger - 02 May 2020 12:43:03
 
You are on the right track. You need to reverse the order of the last part of your expression:

([0-9]|[1-8][0-9]|9[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])

Like this: (25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])

This is because the regex engine will return a match as soon as it finds one. So we need to put the most specific cases first and the most general cases last.
Pages: 1