Andreas Hansen wrote:
i have some trouble with a regex.
it works on rubular.com but not in my program
What regexp language is rubular.com using? Perl, ruby 1.8, ruby 1.9,
other?
regex =
/(?:[I][P]\s)((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:\d{1,3}\.){3}\d{1,3}).{0,}(?:[:][\s])([P])(?:.{0,})$\s(?:^[E].{5}[@].{9}[Q])(?:.{30,42}\.)\W{0,}([\w\d]{1}(?:[\w\d-]+.){1,13}[\wA-Z0-9])/i
This regexp must be machine-written, since it's absolutely horrible.
You'd never write it that way by hand. For example:
(?:[I][P]\s) => is just the same as => IP\s
(?:.{0,})$ => is just the same as => .*$
[\wA-Z0-9] => is just the same as => \w
I suggest you write your regexp by hand, one bit at a time. This is easy
in IRB as you can develop your regexp interactively.
irb(main):008:0> src
=> "12:23:59.378678 IP 85.225.108.54.54707 > 81.227.132.223.6112: P
590518027:590518071(44) ack 2582330461 win
64240\nE..Te.@.t..U.l6Q.......#2....<]P.........,................wakko0..........@......"
irb(main):009:0> /IP / =~ src
=> 16
irb(main):010:0> /IP ([0-9a-fA-F.]+)/ =~ src
=> 16
irb(main):011:0> $1
=> "85.225.108.54.54707"
irb(main):012:0> /IP ([0-9a-fA-F.]+)\.\d+/ =~ src
=> 16
irb(main):013:0> $1
=> "85.225.108.54"
This one isn't quite as sophisticated for IP address matching as the one
rubular gave you, but it's not necessary here. If you really want
stronger matching of IPv4 and IPv6 literals, you can do so if you wish.
···
--
Posted via http://www.ruby-forum.com/\.