How to extract the session id from an RTSP message's content?

java, string, substring

Solution

Simply use a `regular expression` with a group, then extract the value of the group as next:

String content ="RTSP/1.0 200 OK\n" +
   "CSeq: 3\n" +
   "Server: Ants Rtsp Server/1.0\n" +
   "Date: 21 Oct 2016 15:55:30 GMT\n" +
   "Session: 980603187; timeout=60\n" +
   "Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=F006B800\n";
Pattern pattern = Pattern.compile("Session: ([a-zA-Z0-9$\\-_.+]+)");
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

Output:

980603187

Explanation:

Session: ([a-zA-Z0-9$\\-_.+]+)

- `Session:` matches the characters `Session:` literally (case sensitive)

- `([a-zA-Z0-9$\\-_.+]+)`: Capturing group that matches with several consecutive ALPHA, DIGIT or SAFE characters (at least one) (cf RFC 2326 chapter 3.4 Session Identifiers)

Problem

I have a string like this: ``` RTSP/1.0 200 OK CSeq: 3 Server: Ants Rtsp Server/1.0 Date: 21 Oct 2016 15:55:30 GMT Session: 980603187; timeout=60 Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=F006B800 ``` I want to extract the session number(`980603187`) Could someone please provide some help?

Original source