0

Say the url is http://aa/bb/cc. "aa" is in segment 1, "bb" in 2 and "cc" in 3. How could the regex extract the given number of segment? (so it would be something like \2, \3 which refers to that part of URL.)

NSF
  • 2,499
  • 6
  • 31
  • 55
  • 1
    Why must you use a regex? Nearly every framework has a built-in method for doing that; consult http://stackoverflow.com/questions/15390563/. – Dour High Arch Apr 09 '13 at 03:24
  • 1
    **This might not be a job for regexes, but for existing tools in your language of choice.** Regexes are not a magic wand you wave at every problem that happens to involve strings. You probably want to use existing code that has already been written, tested, and debugged. In PHP, use the [`parse_url`](http://php.net/manual/en/function.parse-url.php) function. Perl: [`URI` module](http://search.cpan.org/dist/URI/). Ruby: [`URI` module](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html). .NET: ['Uri' class](http://msdn.microsoft.com/en-us/library/txt7706a.aspx) – Andy Lester Apr 09 '13 at 04:08

2 Answers2

2

Try this regex:

http:/(?:/([^/]+))+

explaination:

(subexpression) Captures the matched subexpression and assigns it a zero-based ordinal number.

(?:subexpression) Defines a noncapturing group.

+ Matches the previous element one or more times.

[^character_group] Negation: Matches any single character that is not in character_group.

Ria
  • 10,237
  • 3
  • 33
  • 60
0

Try this:

http://(.*?)/(.*?)/(.*?)

The regex .*? is a "non-greedy" match.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks but seems I didn't make the question clear enough. Actually say I want to get the third segment which is cc, I want the regex to return only "cc" but not an array of [aa, bb, cc]. – NSF Apr 09 '13 at 02:59
  • With this regex, the segment "cc" is accessible as \3 in a replacement expression. You haven't indicated what language you use, so it's hard to help more. I am not aware of any language that would return them as an array as your comment suggests – Bohemian Apr 09 '13 at 03:07