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 @
?
Asked
Active
Viewed 2.4k times
4
-
2The title of the question is misleading – mbomb007 Sep 23 '16 at 14:01
-
1Who 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 Answers
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