0

I have a url and I am trying to parse a string from it.

https://www.example.com/files/filename1/whop-die-do/434ssdf/rewth

The url will always be: https://www.example.com/files

The string I am trying to find is: filename1/whop-die-do

So other examples would look like this:

https://www.example.com/files/filename2/apples/asdfasdf/reasdfasdfth
   \---> filename2/apples
https://www.example.com/files/bloop/oranges_r_great/zcvr/asdfasd
   \---> bloop/oranges_r_great
https://www.example.com/files/name4/Wowzers123/as4t/532sadf
   \---> name4/Wowzers123

This is the current regex pattern I am using /filename2\/([^/]+)/ (courtesy of @Wiktor)

This works only if the it contains filename2 within the string. Is it possible to have it automatically match after the: https://etcetc

Thank you.

Syn
  • 384
  • 2
  • 12
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Jan 23 '20 at 23:33
  • This seems like a bit more of an advanced regex so its hard to get started. I know very basic regex but finding and returning is pretty new for me. – Syn Jan 23 '20 at 23:41
  • It is a basic regex. Negated character class, capturing group and anchors will do. – Wiktor Stribiżew Jan 23 '20 at 23:45
  • Ok ill looks those up for now – Syn Jan 24 '20 at 00:11
  • @WiktorStribiżew So... I was able to get something like this: `\b(\w*files\/\w*\w)\b` and it is grabbing the word `files/filename1` but i cant figure out how to grab the next section... – Syn Jan 24 '20 at 00:25
  • See https://regex101.com/r/c503Sh/1, the pattern is `/[^/]*files/([^/]+/[^/]+)` – Wiktor Stribiżew Jan 24 '20 at 00:29
  • Hmm... can you point me a direction to look at? I realized I dont need `files`, I need `filename1/whop-die-do`, `filename2/wowzers`. Is there some regex that says means: start from here.... Really appreciate the help. – Syn Jan 24 '20 at 00:36
  • I saw that I can ignore from the beginning: `^(?!.*https://www.bloop.com/files/).*$(/[^/]+[^/]+)` but I need some help with the syntax – Syn Jan 24 '20 at 00:39
  • I can do: `/[^/]*filename1/([^/]+)` but the `filename1` will change from time to time – Syn Jan 24 '20 at 00:41
  • Once you understand what requirements you have, please update the question. First, rules must be devised, then, you may think of the pattern. Besides, regex flavor/programming language must be specified, add a tag to the question. – Wiktor Stribiżew Jan 24 '20 at 00:54
  • Edited the question to be more concise. – Syn Jan 24 '20 at 01:00
  • You already know `[^/]+`, use it. – Wiktor Stribiżew Jan 24 '20 at 01:03
  • with: `\/([^\/]+\/[^\/]+)` it matches 3x – Syn Jan 24 '20 at 01:12

0 Answers0