Friday 15 July 2011

java - How to build a regular expression for a host name? -



java - How to build a regular expression for a host name? -

this question has reply here:

regular look match dns hostname or ip address? 20 answers

need build regular look following:

x.x.x.x

above represents host name, x grouping of characters/numbers , there must 3 dots.

i tried few things failing in cases.

you can seek one:

string regex = "[a-z0-9]+[\\.]{1}[a-z0-9]+[\\.]{1}[a-z0-9]+[\\.]{1}[a-z0-9]+";

explained:

[a-z0-9]+ matches grouping or characters/numbers minimal length of 1. [\\.]{1} matches 1 . sympol. {1} denotes length of 1, can utilize [.] or \\., well. the above 2 rules repeated several times

as @calvin scherle mentioned, can shorten regex to:

string regex = "\\w+\\.\\w+\\.\\w+\\.\\w+";

explained:

\w+ match every grouping of word characters (including letters , digits) minimal length of 1 \. match . symbol in java, \ metacharacter , has escaped , we'll have \\w+ , \\. the above 2 rules repeated several times

java regex ip

No comments:

Post a Comment