简体   繁体   中英

Regex on picking S3 bucket name from S3 URI

I have been trying to regex pattern to obtain S3 bucket name from S3 URI but have no luck.

example: s3://example-bucket/file-name.filetype

Closest I could get with this: \\/\\/([^\\/].*[^\\/])\\/ but i'm not sure how to negate the slash from the result

The part ([^/]+) is looking for a sequence of of characters that are not slash. keeping close with what you had you could write //([^/]+)/ but this is the same as

//([^/]+)

optional you could use lookbehind (?<=//) and/or lookahead (?=/)

(?<=//)([^/]+)(?=/)

(depending on your use cases a couple of different lookahead expressions are possible)

'lookaround' especially 'lookbehind' is not supported in all regexp dialects. eg not in JavaScript

Debuggex Demo

If you don't have the lookbehind option (javascript regex), and your URL structure is consistent enough, then perhaps this pattern will be useful:

[^\\/]+(?=\\/[^\\/])

In English: "match one or more non-forward slashes, followed (but not actually matched, via the lookahead) by one forward slash and a non-forward slash".

(your other option would be to access your match groups in order to get rid of the slashes that you matched)

https://regex101.com/r/XM05Sw/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM