Regular Expression to collect everything after the last /

Regular Expression to collect everything after the last /

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Regular Expression to collect everything after the last /

Extracting a specific substring from a URL based on its structural components is a common task when dealing with legacy APIs or third-party services like Google's GData. In your specific case, you need to isolate the identifier p1f3JYcCu_cb0i0JYuCu123 from a string like http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123. The challenge lies in defining "everything after the last /".

A naive approach might be to match everything following any slash, but this fails if the target string contains multiple segments. For example, a path like http://example.com/path/to/resource/123 requires capturing only 123, not path/to/resource/123. To solve this, you need a pattern that explicitly anchors to the final delimiter and captures all subsequent characters until the end of the string.

The core logic relies on matching the literal forward slash character (/) followed by any sequence of non-slash characters. In regex syntax, [^\/]+ matches one or more characters that are not a forward slash. Combining these yields the pattern /[^\/]+. When applied globally to a URL string, this captures every segment after each slash. However, since you only want the last segment, you must ensure the engine stops matching at the end of the string.

In PHP, you can leverage preg_match with a capturing group to extract this specific value. The following snippet demonstrates how to isolate the final path component:

$url = 'http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123';

// Pattern: Match / followed by anything that isn't a slash, until end of string
$pattern = '/[^\/]+$/'; 

if (preg_match($pattern, $url, $matches)) {
    $id = $matches[0];
    echo $id; // Outputs: p1f3JYcCu_cb0i0JYuCu123
} else {
    // Handle case where no match is found
    echo 'No ID found';
}

This approach uses the $ anchor to assert the end of the string, ensuring that only the final segment following the last / is returned. If the input URL were http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123?param=value, the simple regex above would return p1f3JYcCu_cb0i0JYuCu123?param=value. Depending on your requirements, you might need to refine the pattern to exclude query parameters. If query strings are a concern, you can adjust the character class to specifically exclude ?:

$pattern = '/[^\/\?\#]+$/';

This modification ensures that if the ID is followed by query parameters or fragments, they are excluded from the result. While regular expressions are powerful, they can become fragile when parsing complex URLs with many optional components. A more robust architectural pattern often involves splitting the URL string by / and taking the last element of the resulting array. This method avoids the overhead of compiling regex patterns for every request and makes the logic explicit and easier to debug.

$parts = explode('/', $url);
$id = end($parts);

This alternative is generally preferred in production environments unless you are parsing thousands of strings where regex performance is critical, or if the input format is strictly guaranteed to be a URL with no query parameters. Using explode separates concerns cleanly: the string manipulation handles the path, while your business logic handles the extracted ID.

For those working within the Laravel ecosystem, similar utility exists in the form of the Str::afterLast helper method, which abstracts this specific regex logic away from you. If you are building a larger application where URL parsing is part of a broader data ingestion pipeline, consider whether existing framework helpers or dedicated libraries might reduce technical debt compared to maintaining custom regex patterns.

Ultimately, the choice between a regex and a split operation depends on your specific constraints regarding query strings and performance. Both methods achieve the goal of isolating the identifier, but understanding the underlying mechanics allows you to choose the right tool for the job without over-engineering the solution.

Our network