tags:

views:

178

answers:

5

What regex can take any of the lines below as input

rtsp://server/blabla/bla RTSP/1.0
rtsp://server/blabla/bla/
rtsp://server/blabla/bla
rtsp://server/blabla/bla/streamid=65335 RTSP/1.0

and always returns:

rtsp://server/blabla/bla

In general I have an arbitrary URL which always starts with "rtsp://" and optionally ends with EOL, "/", " RTSP/1.0" or "/streamid=65335 RTSP/1.0".

I need to get the URL only i.e. without the optional trailing parts.

Thanks.

+1  A: 

well this will do exactly what you just asked for:

$pattern = '/rtsp:\/\/server\/.*/'
$replace = 'server/9C8CE56C490F2C87';

preg_replace($pattern, $replace, 'rtsp://server/blabla/bla RTSP/1.0');

If you want to get everything before the space this will work:

$pattern = '/([^ ]+).*/'

preg_replace($pattern, '$1', 'rtsp://server/blabla/bla RTSP/1.0');
John Isaacks
I need the whole URL. But the URL without all the trailing part. Please, see my edited post at the top again.
Jack
A: 

Are you looking for something like this:

^rtsp://([^/]+)

And then replacing it by “$1/9C8CE56C490F2C87”?

Gumbo
+2  A: 

This should capture the server name.

/rtsp:\/\/([^\/]+)/

from new requirements ( this removes trailing ):

linux ~ $ echo "rtsp://server/blabla/bla RTSP/1.0
rtsp://server/blabla/bla/
rtsp://server/blabla/bla
rtsp://server/blabla/bla/streamid=65335 RTSP/1.0
" | perl -pe 's/( RTSP\/1.0|\/|\/streamid=65335 RTSP\/1\.0)$//g'
rtsp://server/blabla/bla
rtsp://server/blabla/bla
rtsp://server/blabla/bla
rtsp://server/blabla/bla

This one captures url:

echo "rtsp://server/blabla/bla RTSP/1.0
rtsp://server/blabla/bla/
rtsp://server/blabla/bla
rtsp://server/blabla/bla/streamid=65335 RTSP/1.0
" | perl -pe 's/(.+?)(?: RTSP\/1.0|\/|\/streamid=65335 RTSP\/1\.0)$/\1/'
rtsp://server/blabla/bla
rtsp://server/blabla/bla
rtsp://server/blabla/bla
rtsp://server/blabla/bla
sfossen
I need the whole URL, not just the server. But the URL without all the trailing part. Please, see my edited post at the top again.
Jack
+1  A: 

You could match against:

(.*?)(\/streamid\=65335)?(\/)?( RTSP\/1.0)?(\n)?$

and read only the first group.

Depending on what language you are using it might be clearer to use string-processing. For example in Python:

for suffix in ('\n', ' RTSP/1.0', '/streamid=65335', '/'):
    if url.endswith(suffix):
        url= url[:-len(suffix)]
bobince
I agree he shouldn't look to a regex in this case, but he's using C (or C++?) for what it's worth.
Chris Lutz
+1  A: 

Based on your new criteria, I don't think you need a regex.

EDIT: Removed Perl solution, since you're using C. Using C, do this:

str[20] = 0; // string is now NUL-terminated to 20 characters
if(!strcmp(str, "rtsp://server/blabla/bla"))
  {
    // do stuff if it matches
  }
else
  {
    // do stuff if it doesn't match
  }

Or, if you want to keep the original string:

if(!strncmp(str, "rtsp://server/blabla/bla", 20)) // only compare 20 chars
  {
    // do stuff if it matches
  }
else
  {
    // do stuff if it doesn't match
  }

You may want to do the strncmp() solution. That way, you can vary the length on a query to query basis.

Chris Lutz
sorry but I'm using GNU C library regex functions
Jack