Java 用于SIP和SIPS URI的正则表达式

Java 用于SIP和SIPS URI的正则表达式,java,regex,regex-negation,regex-group,Java,Regex,Regex Negation,Regex Group,示例SIPURI sip:alice@atlanta.com sip:alice:secretword@atlanta.com;transport=tcp sips:alice@atlanta.com?subject=project%20x&priority=urgent sip:+1-212-555-1212:1234@gateway.com;user=phone sips:1212@gateway.com sip:alice@192.0.2.4

示例SIPURI

   sip:alice@atlanta.com
   sip:alice:secretword@atlanta.com;transport=tcp
   sips:alice@atlanta.com?subject=project%20x&priority=urgent
   sip:+1-212-555-1212:1234@gateway.com;user=phone
   sips:1212@gateway.com
   sip:alice@192.0.2.4
   sip:atlanta.com;method=REGISTER?to=alice%40atlanta.com
   sip:alice;day=tuesday@atlanta.com
我创建的正则表达式
^(sip|sips):([^@]+)@(+.+)

我试图实现的是@是可选的,如果@在@之前和之后有什么东西应该存在,否则在sip之后:任何东西都可以被接受

您可以使用

^(sips?):([^@]+)(?:@(.+))?$

详细信息

  • ^
    -字符串的开头
  • (sips?
    -第1组:
    sip
    sips
  • -冒号
  • ([^@]+)
    -第2组:除
    @
  • (?:@(+)?
    -可选的非捕获组:
    • @
      -一个
      @
      字符
    • (.+)
      -第3组:除换行符以外的任何0+字符,尽可能多
  • $
    -字符串结束

注意:如果将模式与
.matches()
方法一起使用,
^
$
是多余的,可以从模式中删除,因为该方法需要完整的字符串匹配。

我的答案对您有用吗?如果你愿意,请考虑接受。