Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Community > Forum De Bell Tolls > [java] validare IP e domain con le regex
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
[java] validare IP e domain con le regex

ho fatto un copia incolla del mio post sul forum della sun, non avevo molta voglia di tradurre, tanto si capisce il problema...no?

Non sono nemmeno tanto sicuro di avere fatto le regexp giuste, se notate qualche errore me lo dite?


this is the code I use but I can't set EXP2 to limit the number under the 256. Does anybody help?

P.S.
EXP1 validate a domain name (es. www.myserver.com)
EXP2 may validate IP address (es. 111.111.111.0)

code:
public static boolean isServerAddress(String s){ System.out.print(s + " "); String EXP1 ="([a-zA-Z0-9]+).([a-zA-Z0-9]+).([a-zA-Z]{2,3})|([a-zA-Z0-9]+).([a-zA-Z0-9]+).([a-zA-Z]{2,3}).([a-zA-Z]{2,3}+)"; String EXP2 ="([0-9]{1,3}+).([0-9]{1,3}+).([0-9]{1,3}+).([0-9]{1,3})"; if(Pattern.matches(EXP1, s)){ return true; }else if(Pattern.matches(EXP2, s)){ return true; }else{ return false; } }


__________________
http://www.twitter.com/0m4r

27-08-2003 16:16
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Mino
In pensione :)

User info:
Registered: Mar 2002
Posts: 2997 (0.34 al dì)
Location: Dublin!
Corso: Informatica 3ennale
Anno: Laureato :)
Time Online: 12 Days, 2:11:07: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

premetto che non so nulla di come si usano le regex in java :)

cmq, un ip lo puoi matchare così:
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/
oppure
/(\d{1,3}\.\d{1,3}){2}\/
oppure
/((\d{1,3}\.){3}\d{1,3}/

ricordati sempre che il punto va backslashato altrimenti significa "un carattere qualunque"...

Per il dominio io farei
/([a-zA-Z0-9]+\.)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,4}/
in modo che funzioni anche se contiene più di 2 pezzi, ad esempio
dominio.com
www.dominio.com
www.usr.dominio.com
ecc ecc

però per fare una cosa fatta bene dovresti anche validare l'ultimo gruppetto con i tld (top level domain, ad esempio "it", "com", "org", ecc) validi...
spero di aver azzeccato le parentesi.

edit: sto coso ovviamente non controlla indirizzi limite tipo 333.333.333.333

__________________
icq: 57553717 - mail: mino@ngi.it - web: www.minux.it - Fattori Arcani

Last edited by Mino on 27-08-2003 at 16:52

27-08-2003 16:46
Click Here to See the Profile for Mino Click Here to See the Blog of Mino Visit Mino's homepage! Find more posts by Mino Add Mino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by Mino

edit: sto coso ovviamente non controlla indirizzi limite tipo 333.333.333.333


per min:0)
prima di tutto grazie, poi mi spiegeresti un secondo cosa stai dicento con quelle regex che hai proposto?


per tutti:
idee per risolvere questo problema?

__________________
http://www.twitter.com/0m4r

27-08-2003 18:22
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Mino
In pensione :)

User info:
Registered: Mar 2002
Posts: 2997 (0.34 al dì)
Location: Dublin!
Corso: Informatica 3ennale
Anno: Laureato :)
Time Online: 12 Days, 2:11:07: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

per tutti:
idee per risolvere questo problema? [/B]

non ho capito... utilizza quelle, sono corrette!
in pratica
\d equivale a [0-9]
\. è il "punto"
{1,3} vuol dire "da uno a tre caratteri"
le parentesi tonde servono per decidere a cosa applicare l'operatore che segue
+ significa 1 o più volte
* significa 0 o più volte
? significa 0 o 1 volta
(hhihihi... scusa, come minicorso di regex fa un po' schifo :D )

devi metter tutta la regexp tra / /

infine, come dicevo, la regexp degli ip funziona anche con casi tipo
999.655.445.321, cioè guarda se sono 4 gruppi di 3 numeri, ma non controlla che siano >=0 e <=255, per fare questo hai due strade:
- o complichi la regexp
- o "parsi" quella stringa e la controlli

__________________
icq: 57553717 - mail: mino@ngi.it - web: www.minux.it - Fattori Arcani

28-08-2003 08:58
Click Here to See the Profile for Mino Click Here to See the Blog of Mino Visit Mino's homepage! Find more posts by Mino Add Mino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

come si complica la regex per fare quei controlli? è questo che ti chiedevo!

Off-Topic:
il tuo corso è stato molto piu utili di alcuni che ho seguito in questi 3 anni! ;P

__________________
http://www.twitter.com/0m4r

28-08-2003 10:44
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Mino
In pensione :)

User info:
Registered: Mar 2002
Posts: 2997 (0.34 al dì)
Location: Dublin!
Corso: Informatica 3ennale
Anno: Laureato :)
Time Online: 12 Days, 2:11:07: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

su google ho trovato un tizio che propone:
/(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])/

è assolutamente illeggibile e probabilmente non molto elegante ma è corretta... In pratica ogni blocco è
\d => da 0 a 9
oppure
[1-9]\d => da 10 a 19
oppure
1\d\d => da 100 a 199
oppure
2[0-4]\d => da 200 a 249
oppure
25[0-5] => da 250 a 255

ma forse ti conviene fare cose più grezze tipo:
- guarda quanti "." appaiono nella stringa, se sono !=4 errore.
- prendi la stringa dalla posizione 0 alla posizione del primo "." -1 e parsala in int e controlla che sia >0 e <255, altrimenti errore
ecc ecc

edit: aggiungo, comunque dovresti controllare anche che siano ip non validi, per esempio
0.0.0.0
224-239.*.*.* (multicast)
240-254.*.*.* (riservati)
255.255.255.255
e le varie classi private:
10.*
127.*
172.16.* fino a 172.31.*
192.168.*

__________________
icq: 57553717 - mail: mino@ngi.it - web: www.minux.it - Fattori Arcani

Last edited by Mino on 28-08-2003 at 10:58

28-08-2003 10:50
Click Here to See the Profile for Mino Click Here to See the Blog of Mino Visit Mino's homepage! Find more posts by Mino Add Mino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

/([a-zA-Z0-9]+\.)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,4}/


perche mi da valido anche 111.222.333.sss? io non voglio

__________________
http://www.twitter.com/0m4r

28-08-2003 10:53
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by Mino


ma forse ti conviene fare cose più grezze tipo:
- guarda quanti "." appaiono nella stringa, se sono !=4 errore.
- prendi la stringa dalla posizione 0 alla posizione del primo "." -1 e parsala in int e controlla che sia >0 e <255, altrimenti errore
ecc ecc


stavo pensando anche io di fare cosi

__________________
http://www.twitter.com/0m4r

28-08-2003 10:54
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Mino
In pensione :)

User info:
Registered: Mar 2002
Posts: 2997 (0.34 al dì)
Location: Dublin!
Corso: Informatica 3ennale
Anno: Laureato :)
Time Online: 12 Days, 2:11:07: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Originally posted by 0m4r
perche mi da valido anche 111.222.333.sss? io non voglio


guarda che quella regexp te l'ho scritta per validare domini, non ip. In tal caso quali sono le limitazioni che vuoi imporre?

ocio che ho editato il post prima che rispondessi

__________________
icq: 57553717 - mail: mino@ngi.it - web: www.minux.it - Fattori Arcani

28-08-2003 10:59
Click Here to See the Profile for Mino Click Here to See the Blog of Mino Visit Mino's homepage! Find more posts by Mino Add Mino to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
0m4r
.grande:maestro.

User info:
Registered: Mar 2002
Posts: 7287 (0.84 al dì)
Location: Düsseldorf (DE)
Corso:
Anno: ESAMI FINITI
Time Online: 49 Days, 0:57:33 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

io vogli essere sicuro che quello che mi viene passato come parametro sia un indirizzo valido, senza sapere a priori se mi sarà inserita l'url (www.dsy.it) oppure l'indirzzo IP (000.000.000.000).

code:
public static boolean isServerAddress(String s){ String EXP1 ="([a-zA-Z0-9]+)\\.([a-zA-Z0-9]+)\\.([a-zA-Z]{2,3})|"; EXP1 += "([a-zA-Z0-9]+)\\.([a-zA-Z0-9]+)\\.([a-zA-Z]{2,3})\\.([a-zA-Z]{2,3}+)|"; EXP1 += "([a-zA-Z0-9]+\\.)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,4}"; String EXP2 ="([0-9]{1,3}+)\\.([0-9]{1,3}+)\\.([0-9]{1,3}+)\\.([0-9]{1,3})"; if(Pattern.matches(EXP1, s)){ System.out.println("1 " + s); return true; }else if(Pattern.matches(EXP2, s)){ System.out.println("2 " + s); return true; }else{ System.out.println("3 " + s); return false; } }

__________________
http://www.twitter.com/0m4r

28-08-2003 11:02
Click Here to See the Profile for 0m4r Click Here to See the Blog of 0m4r Click here to Send 0m4r a Private Message Find more posts by 0m4r Add 0m4r to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 16:08.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.261 seconds (42.82% PHP - 57.18% MySQL) con 25 query.