What is a regex for checking jabber id?

Now I use this regex:

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

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

For reference, Section 3 of the main XMPP standard defines the JID in the augmented Backus-Naur form as

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

Your regular expression is incorrect, at least in the following points:

  • This requires jid to contain '@', although jids without '@' can also be valid.
  • ( , , : " JID 1023 " )

, - . - , jid (, node, ), . :

  • ( unit test )
  • .
+5

:

^(?:([^@/<>'\"]+)@)?([^@/<>'\"]+)(?:/([^<>'\"]*))?$

, , , JID, . , JID, 1 node, , 3 - .


:

foo                 (None,  'foo', None)
foo@example.com     ('foo', 'example.com', None)
foo@example.com/bar ('foo', 'example.com', 'bar')
example.com/bar     (None,  'example.com', 'bar')
example.com/bar@baz (None,  'example.com', 'bar@baz')
example.com/bar/baz (None,  'example.com', 'bar/baz')
bär@exämple.com/bäz ('bär', 'exämple.com', 'bäz')

: (?:), , .

+7

Source: https://habr.com/ru/post/1716381/


All Articles