Endoro wrote:@penpen our codes might be suboptimal but working for most real cases, but yours doesn't work ever.
Code: Select all
grep -Po "((\d+){3}\.){3}(\d+){3}(:(\d+){5})?" "file"
This code indeed does not work on most cases, and it even produces wrong positives as your code.
But it recognizes all above 000.000.000.000 or 000.000.000.000:00000 (inclusively), so it recognizes nearly almost 1/64 of all possible valid representations, (and all above, as your solution).
Because of this i've written: "Although i'm not firm with grep, so better someone who is, should have a view on it".
I thought, what i've wanted to do is obvious namely (now i am firm with grep):
Code: Select all
grep -Po "(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?" "textfile.txt"
Endoro wrote:But if you dont't want to find '299.222.7.123:8080' you need more than Regex, you need the ability to calculate.
This is wrong for a finite set of words over a language.
So it is always possible to realize this with one regular expression;
the following regexp is a little bit longer but it should be the fastest on internal structure build within grep and on execute:
Code: Select all
grep -Po -w "(((\d{1,2})|([0-1]\d{2})|(2(([0-4]\d)|(5[0-5]))))\.){3}((\d{1,2})|([0-1]\d{2})|(2(([0-4]\d)|(5[0-5]))))(\:((\d{1,4})|(([0-5]\d{4})|(6(([0-4]\d{3})|(5(([0-4]\d{2})|(5(([0-2]\d)|(3[0-5]))))))))))?" "file"
Note that i didn't tried to offend anybody with my last post, it should be just a hint.
penpen
Edit: Fixed some spelling flaws.
Edit2: Added the edit notes.