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?
CodePudding user response:
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
CodePudding user response:
Your issues
(\w*?/\w*?)$ is for simple or empty last 2 segments (tested), e.g.
- matched
hello/world/subscriptions123/snap_shotscapturingsubscriptions123/snap_shots - 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 asfor at least one (instead*for any or?for zero or one)\wis for word-meta-character, does not match hyphens or dots (OK forsnapshot, 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
