Sign in to follow this  
Followers 0
Juanola

StringRegExp IP pattern

16 posts in this topic

I use the pattern:

'(\d{1,3}\.){3}\d{1,3}:[0-9]{5}|(\w\.){2}\w:[0-9]{5}'

for find strings as:

192.45.234.1:66000

192.168.122.322:74000

san4.ukr.net:32000

.............

.............

But stringregexp isn't finding nothing.

Is my pattern ok?

Thank you!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include<array.au3>
$reg=StringRegExp("127.0.0.1:67776","([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\:([0-9]{1,5})",3)
_ArrayDisplay($reg)

Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include<array.au3>
$reg=StringRegExp("127.0.0.1:67776","([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\:([0-9]{1,5})",3)
_ArrayDisplay($reg)
Thank you!!!

But and the string "san4.ukr.net:32000" ?

My pattern is bad?

I think what my pattern is OK.

Edited by Juanola

Share this post


Link to post
Share on other sites

Two examples from RegexBuddy

IP address (accurate capture)

Matches 0.0.0.0 through 255.255.255.255

Use this regex to match IP numbers with accurracy.

Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.

"\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"oÝ÷ ØÚuÚÞ²ÆrêÚµãµÈ^³M4ÒØk¢è!Ûvç¹çnyRÇ­++z±¶µÈH>{¦mêìÂ+aiÇ.®¶Ë­­iDzËh¶¢Ø¯ÛçºfޮƮ¶­sbgV÷C²b3#²b3#¶"ó¢ó£#U³ÓU×Ã%³ÓEÕ³Ó×ųÓõ³ÓÕ³ÓÓòb3#²b3#²â³7Òó£#U³ÓU×Ã%³ÓEÕ³Ó×ųÓõ³ÓÕ³ÓÓòb3#²b3#¶"gV÷C

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you!!

It is one great and professional answer, but I want find IP with numbers and with letters.

I want too find IP's as:

san4.ukr.net:32000

Your pattern find IP's as 192.168.22.111

But I too want find IP's as "san4.ukr.net:32000"

|(\w\.){2}\w:[0-9]{5}'

OR san4.ukr.net:32000

Is my pattern bad? Why I can't find nothing with my pattern?

'(\d{1,3}\.){3}\d{1,3}:[0-9]{5}|(\w\.){2}\w:[0-9]{5}'

Thank you!!!

Edited by Juanola

Share this post


Link to post
Share on other sites

Thank you!!

It is one great and professional answer, but I want find IP with numbers and with letters.

I want too find IP's as:

san4.ukr.net:32000

Your pattern find IP's as 192.168.22.111

But I too want find IP's as "san4.ukr.net:32000"

|(\w\.){2}\w:[0-9]{5}'

OR san4.ukr.net:32000

Is my pattern bad? Why I can't find nothing with my pattern?

'(\d{1,3}\.){3}\d{1,3}:[0-9]{5}|(\w\.){2}\w:[0-9]{5}'

Thank you!!!

I don't see anything wrong with your pattern except that it doesn't work.

This works though

$test = '((\d{1,3}\.){3}\d{1,3}:[0-9]{4,6})|\w*\.\w*\.\w*:[0-9]{5}'
$s = "san4.ukr.net:32000"
$r = stringregexp($s,$test,1)
ConsoleWrite($r[0] & @CRLF)
$s = "192.45.234.1:66000"
$r = stringregexp($s,$test,1)
ConsoleWrite($r[0] & @CRLF)

I tried a lot of combinations with groups and repeating characters but it seems that when you use the OR symbol a lot of things after the OR symbol don't work as you would expect, or there is something we don't understand.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I think one of the problems is noone is escaping the ':'s, the other is probably that \w doesn't seem to catch words with numbers in them?

In any case, this seems to works from my tests:

$sString="192.45.234.1:66000" & @CRLF & "192.168.122.322:74000" & @CRLF & "san4.ukr.net:32000"
$aIPArray=StringRegExp($sString,"((?:\d{1,3}\.){3}\d{1,3}\:[0-9]{5}|(?:[[:alnum:]]+\b\.){2}[[:alnum:]]+\b\:[0-9]{5})",3)

Share this post


Link to post
Share on other sites

lol, Do you aware that this pattern match insane IP addresses as well?

$sString="192.45.234.109090909090:66000" & @CRLF & "192.168.122.3226923212yy312123a:74000" & @CRLF & "san4.ukr.net:32000"

You should not use greedy quantifiers if they will match through the whole string part and backtrack to a valid spot where it can match unwanted stuff. You don't have to preform it all in one line...

Share this post


Link to post
Share on other sites

lol, Do you aware that this pattern match insane IP addresses as well?

$sString="192.45.234.109090909090:66000" & @CRLF & "192.168.122.3226923212yy312123a:74000" & @CRLF & "san4.ukr.net:32000"

You should not use greedy quantifiers if they will match through the whole string part and backtrack to a valid spot where it can match unwanted stuff. You don't have to preform it all in one line...

I didn't see those types of strings in the example provided, nor did Juanolo indicate if there were invalid IP strings in whatever they are checking.

Sure, Juanola can get an array of # IP addresses first, but how do you propose a solution for websites that *can* contain many of those crazy strings. Should it end in official '.com,.net, and a million other top-level domains'? That would be one long string. Maybe if Juanola could narrow down what top-level domain names, the end can be a simple \.(net|com|org)\:\d{5}

Share this post


Link to post
Share on other sites

Probably it can take time although the solution is obvious and a short search might come up with a fast solution.

About the hostpath and the top-level domain it's not a task for RegEx alone as it's require an external resource like a csv file that contain the valid 2 letters country code and the .info, .com, .org, .mail is quite limited for this purpose so it can be examined using alternations.

Share this post


Link to post
Share on other sites

What you are actually looking for is a hostname match and a port number. IP addresses are dotted quads (0.0.0.0).

When you want to match words, there's nothing you can do to stop it from matching crazy IP addresses.

Share this post


Link to post
Share on other sites

Correct me if I'm wrong but ping-ing the number first off then doing your check will comeout with a number form instead of alpha-numerical


0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Share this post


Link to post
Share on other sites

It would be better to just run the passed entry to a DNS server. It will return an IP address for a hostname, and for an IP address.

Share this post


Link to post
Share on other sites

I use the pattern:

'(\d{1,3}\.){3}\d{1,3}:[0-9]{5}|(\w\.){2}\w:[0-9]{5}'

for find strings as:

192.45.234.1:66000

192.168.122.322:74000

san4.ukr.net:32000

.............

.............

But stringregexp isn't finding nothing.

Is my pattern ok?

Thank you!

This appears to work.

#include<array.au3>

$sString = "192.45.234.1:66000 192.168.122.322:74000 san4.ukr.net:32000" & @CRLF & "san4.ukr.net:32000"

$reg = StringRegExp($sString, "((?:\w{1,3}\.\w{1,3}\.\w{1,3}\.\w{1,3}:\w{5})|(?:\w{1,}\.\w{1,}\.\w{1,}:\w{5}))", 3)

_ArrayDisplay($reg)

Share this post


Link to post
Share on other sites

what about if you have microsoft.net:30126 that would not do it, you would need a numerical number such as 207.46.197.32:30126 as that applies to microsoft.net. You need to pass the whole address into probably a string and dll call winnet for a change in you address to ensure it is numerical else it will error out


0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Share this post


Link to post
Share on other sites

what about if you have microsoft.net:30126 that would not do it, you would need a numerical number such as 207.46.197.32:30126 as that applies to microsoft.net. You need to pass the whole address into probably a string and dll call winnet for a change in you address to ensure it is numerical else it will error out

So are you saying this one would not work either?

#include<array.au3>

$sString = "A list of IP's :- 192.45.234.1:66000 192.168.122.322:74000 san4.ukr.net:32000" & @CRLF & _
        "san4.ukr.net:32000 and another, microsoft.net:30126 and that is all."

$reg = StringRegExp($sString, "([\w+\.]+:\w{5})", 3)

_ArrayDisplay($reg)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0