Regular expression – What is the regular expression of the Jabber ID?

Now I am using this regular expression:

^\A([a-z0-9\.\ -_\+]+)@((?:[-a-z0-9]+\.)+[az]{2,})\Z$

I think this is not very good. So what is the best regular expression you have or have seen when validating jids?

As a reference, Section 3 of the XMPP core standard defines a JID in the Augmented Backus-Naur Form as a reference

jid = [node "@ "] domain ["/" resource ]
domain = fqdn / address-literal
fqdn = (sub-domain 1*("." sub-domain))
sub-domain = ( internationalized domain label)
address-literal = IPv4address / IPv6address

your regular expression At least the following points are wrong:

>It requires jid to contain’@’, although jids without’@’ may also be valid.
>It does not check the maximum length (but The link you provided says “The length of each allowed part of the JID must not exceed 1023 bytes”)

I think it’s the wrong way to have a huge regular expression. You’d better write some code, Divide the jid into smaller parts (domains, nodes, resources), and then check each part. It will be better in multiple ways:

>Easier to test (you can individually test each part Unit test)>Better performance>Simpler code>Reusability>etc

Now I am using this regular expression:

^\A([a-z0-9\.\-_\+]+)@(((?:[-a-z0-9]+\.)+[ az]{2,})\Z$

I think this is not very good. So what is the best regular expression you have or have seen when verifying jids?

As a reference, Section 3 of the XMPP core standard defines a JID in the Augmented Backus-Naur Form as a reference

jid = [node "@ "] domain ["/" resource ]
domain = fqdn / address-literal
fqdn = (sub-domain 1*("." sub-domain))
sub-domain = ( internationalized domain label)
address-literal = IPv4address / IPv6address

Your regular expression is wrong in at least the following points:

>It requires jid to contain’@’, although jids without’@’ may also be valid.
>It does not check the maximum length (but the link you provided shows “each allowed part of JID The length of jid must not exceed 1023 bytes”)

I think having a huge regular expression is the wrong way. You’d better write some code to divide jid into smaller parts (domain, node, Resources), and then check each part. It will be better in many ways:

>Easier to test (you can unit test each component separately)>Better performance>Simpler Code>reusability>etc

Leave a Comment

Your email address will not be published.