MehdiPasha
MehdiPasha

Reputation: 23

Extract the last path-segments of a URI or path using RegEx

I am trying to extract the last section of the following string :

"/subscriptions/5522233222-d762-666e-555a-e6666666666/resourcegroups/rg-sql-Belguim-01/providers/Microsoft.Compute/snapshots/vm-sql-image-v3.3-pre-sysprep-Oct-2021-BG"

I want to capture:

"snapshots/vm-sql-image-v3.3-pre-sysprep-Oct-2021-BG"

I tried below with no luck:

(\w*?\/\w*?)$

How to pull this off using regex?

Upvotes: 2

Views: 471

Answers (2)

hc_dev
hc_dev

Reputation: 9377

Your issues

(\w*?/\w*?)$ is for simple or empty last 2 segments (tested), e.g.

  1. matched hello/world/subscriptions123/snap_shots capturing subscriptions123/snap_shots
  2. matched /1/2// capturing the last 2 empty segments

OK was:

  • capture-group
  • / to match the last path-separator before end ($)
  • \w*? intended to match the path-segment of any length

What to improve:

  • *? is a bit too unrestricted, choose quantifier as + for at least one (instead * for any or ? for zero or one)
  • \w is for word-meta-character, does not match hyphens or dots (OK for snapshot, not for given last segment)

Quick-fixed

(\w+/[\w\.-]+)$ (tested)

  • added dot \. and hyphen - to character-set containing \w

Simple but solid

(snapshots/[^\/]+)$ (tested)

  • fore-last path-segment assumed as fix constant snapshots
  • [^\/] any character except (^) slash in last segment

Note: the slash doesn't need to be escaped \/ like Ryszard answered

Upvotes: 0

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use

[^\/]+\/[^\/]+$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  [^\/]+                   any character except: '\/' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  [^\/]+                   any character except: '\/' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Upvotes: 1

Related Questions