Reputation: 23
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
Reputation: 9377
(\w*?/\w*?)$
is for simple or empty last 2 segments (tested), e.g.
hello/world/subscriptions123/snap_shots
capturing subscriptions123/snap_shots
/1/2//
capturing the last 2 empty segmentsOK was:
/
to match the last path-separator before end ($
)\w*?
intended to match the path-segment of any lengthWhat 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)(\w+/[\w\.-]+)$
(tested)
\.
and hyphen -
to character-set containing \w
(snapshots/[^\/]+)$
(tested)
snapshots
[^\/]
any character except (^
) slash in last segmentNote: the slash doesn't need to be escaped \/
like Ryszard answered
Upvotes: 0
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