4

How I can get part of SIP URI? For example I have URI sip:username@sip.somedomain.com, I need get just username and I use [^sip:](.*)[$@]+ expression, but appeared result is username@. How I can exclude from matching @?

starball
  • 20,030
  • 7
  • 43
  • 238
NikedLab
  • 883
  • 4
  • 11
  • 31
  • 2
    The title of the question is misleading – mbomb007 Sep 23 '16 at 14:01
  • 1
    Who was looking for a regex to match middle characters in a string: [How to match the middle character in a string with regex?](https://stackoverflow.com/questions/28051651/how-to-match-the-middle-character-in-a-string-with-regex), [finding middle character in string using regex only](https://stackoverflow.com/questions/17313214/finding-middle-character-in-string-using-regex-only) – bobble bubble Dec 29 '22 at 13:55

3 Answers3

8

this should do the job

(?<=^sip:)(.*)(?=[$@])
VladL
  • 12,769
  • 10
  • 63
  • 83
1

Use a lookahead instead of actually matching @:

^sip:(.*?)(?=@|\$)

Either you are using a very strange regex flavor, or your starting character class is a mistake. [^sip:] matches a single character that isn't any of s,i,p or :. I am also not certain what the $ character is for, since that isn't a part of SIP syntax.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1

If lookaheads are not available in your regex flavour (for instance POSIX regexes lack them), you can still match parts of the string in your regex you don't eventually want to return, if you use capture groups and only grab the contents of some of them.

For example

^sip:(.*?)[$@]+ Then only return the contents of the first capture group

Patashu
  • 21,443
  • 3
  • 45
  • 53