1. Pattern matching
- Posted by thomasj866 at yahoo.com Apr 15, 2004
- 564 views
Hi, I need to find IP in a string. Using wildcard_match() in wildcard.e i.e wildcard_match("*.*.*.*", string) only return true or false. I want it to return position and if possible test if it is legal. thanks Tj
2. Re: Pattern matching
- Posted by matthewwalkerlewis at yahoo.com Apr 15, 2004
- 513 views
> I need to find IP in a string. > Using wildcard_match() in wildcard.e > i.e wildcard_match("*.*.*.*", string) only return true or false. > I want it to return position and if possible test if it is legal. You could try tokenizing the string on '.' and checking the number of fields. You could use Kat's strtok.e for this: http://www.tiggrbox.info/program/strtok-v2-1.html There are also some regular expression libraries in the archives that could probably be used. Something like: /^(\d+\.){3}\d+$/ should work (assuming Perl-compatible regexp). Matt Lewis
3. Re: Pattern matching
- Posted by "Kat" <gertie at visionsix.com> Apr 15, 2004
- 515 views
On 15 Apr 2004, at 4:12, Guest wrote: > > > posted by: matthewwalkerlewis at yahoo.com > > > > I need to find IP in a string. > > Using wildcard_match() in wildcard.e > > i.e wildcard_match("*.*.*.*", string) only return true or false. > > I want it to return position and if possible test if it is legal. > > You could try tokenizing the string on '.' and checking the number of fields. > You could use Kat's strtok.e for this: > > http://www.tiggrbox.info/program/strtok-v2-1.html Assuming the string is a sentence or database, with "words" separated by spaces or commas or such, using wildtok() will do nicely to get the matching token. You can specify getting all the "*.*.*.*" matches, or the first one, 2nd one, etc. If you are parsing a sentence, specify all separators likely to be seen around the ip#, but not '.' , of course. The last example in the html shows using multiple * in a pattern match on a string. If you want only the location of the token, use wildntok(). If the possible matches are complicated, then use Mathew's suggestion on field counting, use toknum(), parsing on '.' in the return from wildtok(), as you know, there should be 4 on a legal match. You won't match the ip# in "This is 127.0.0.1.", because of the period at the end of that sentence, so change that period (delete it). I wasn't trying to implement regexp in wildtok(), but that's an interesting thought! Kat
4. Re: Pattern matching
- Posted by Pete <pete_stoner at btconnect.com> Apr 16, 2004
- 542 views
wrote: > I want it to return position and if possible test if it is legal. > > thanks > Tj >
5. Re: Pattern matching
- Posted by Pete <pete_stoner at btconnect.com> Apr 16, 2004
- 526 views
wrote: > I want it to return position and if possible test if it is legal. > > thanks > Tj > A little late but I have a routine that I did to check if an IP address was a valid format. It checks for no more than 3 '.'s in the string, also checks it is only numeric, also checks for no greater than 255 in any field and finally strips off any leading zeros. I'm sure it could be written better than this but this was the very first program I did with EU... global function IP_Address_Valid(sequence is_it_valid) atom ip_length, number sequence valid_digits, store, addr_to_strip, stripped_addr trace(1) store={} valid_digits = ".1234567890" ip_length = length(is_it_valid) -- Check if it a valid IP address (as much as possible!) if ip_length < 7 then return 0 -- it is definitly not an ip address, too short elsif ip_length > 15 then return 0 -- or too long elsif not wildcard_match("*?.*?.*?.*?", is_it_valid) then return 0 -- invalid characters else for x = 1 to ip_length do -- check for valid characters if not find(is_it_valid[x], valid_digits) then return 0 exit elsif is_it_valid[x] = '.' then -- count the "." - is there more than 3? store= append(store,x) if length(store) > 3 then return 0 exit end if end if end for end if --check no number greater than 255 number= value(is_it_valid[1..(store[1]-1)]) if number[2] > 255 then return 0 end if number = value(is_it_valid[store[1]+1..store[2]-1]) if number[2] > 255 then return 0 end if number = value(is_it_valid[store[2]+1..store[3]-1]) if number[2] > 255 then return 0 end if number = value(is_it_valid[store[3]+1..length(is_it_valid)]) if number[2] > 255 then return 0 end if if length(store) != 3 then return 0 end if --break address into sequence of number sections (prior to finding leading zeros) stripped_addr = {} addr_to_strip = {} addr_to_strip = append(addr_to_strip,is_it_valid[1..(store[1]-1)]) addr_to_strip = append(addr_to_strip,is_it_valid[store[1]+1..store[2]-1]) addr_to_strip = append(addr_to_strip,is_it_valid[store[2]+1..store[3]-1]) addr_to_strip = append(addr_to_strip,is_it_valid[store[3]+1..length(is_it_valid)]) for i = 1 to 4 do if length(addr_to_strip[i]) > 3 then return 0 elsif length(addr_to_strip[i]) > 1 and match("0",addr_to_strip[i]) = 1 then if length(addr_to_strip[i]) > 1 and equal('0',addr_to_strip[i][2]) then if length(addr_to_strip[i]) = 2 then stripped_addr = stripped_addr & addr_to_strip[i][2] end if if length(addr_to_strip[i]) = 3 then stripped_addr = stripped_addr & addr_to_strip[i][3] end if if i != 4 then stripped_addr = stripped_addr & "." end if else stripped_addr = stripped_addr & addr_to_strip[i][2..length(addr_to_strip[i])] if i != 4 then stripped_addr = stripped_addr & "." end if end if else stripped_addr = stripped_addr & addr_to_strip[i] if i != 4 then stripped_addr = stripped_addr & "." end if end if end for return stripped_addr end function Pete.
6. Re: Pattern matching
- Posted by Thomas Jansen <thomasj866 at yahoo.com> Apr 16, 2004
- 530 views
Pete wrote: > > > wrote: > > I want it to return position and if possible test if it is legal. > > > > thanks > > Tj > > > A little late but I have a routine that I did to check if an IP address was > a > valid format. It checks for no more than 3 '.'s in the string, also checks it > is > > only numeric, also checks for no greater than 255 in any field and finally > strips > > off any leading zeros. I'm sure it could be written better than this but this > was the very first program I did with EU... Thanks for all thelp it gave me incentive to do my own code. -- parse_ip.e -- include wildcard.e sequence string string = "<html><head><title>Current IP Check</title></head><body>Current IP Address: 127.0.0.1</body></html>" function match_ip(sequence string) integer pos object void pos = 0 for n=1 to length(string) do if wildcard_match("*.*.*.*", string[n..length(string)]) = 0 then void = string[n..length(string)] pos = n exit end if end for void = string[pos-4..length(string)] while find(void[length(void)], ".0123456789") = 0 do void = void[1..length(void)-1] end while while find(void[1], ".0123456789") = 0 do void = void[2..length(void)] end while return void end function ?match_ip(string) puts(1, match_ip(string)) -- TJ