Skip a specific number of characters, and return the desired output, using regex

StackOverflow https://stackoverflow.com/questions/21079918

  •  27-09-2022
  •  | 
  •  

Question

Here is the code:

%2Fdl%2Fac8ee4c1b1ff6f713d3b19969d6c24e3%2F52d2eae9%2Fff2c0ea38304a00b01c3e573babcde109f

What I need is return in 3 separate regular expressions:

  1. ac8ee4c1b1ff6f713d3b19969d6c24e3 that's an easy one: dl%2F(.*?)%2F

  2. 52d2eae9 -- currently I'm using dots (.) to skip the undesired chars, but looking for a slicker way, if possible

  3. ff2c0ea38304a00b01c3e573babcde109 -- same as (2)

Was it helpful?

Solution

If I misunderstood you and you don't need three seperate expressions, then this one will capture all three items in a capturing group:

dl%2F(.+)%2F(.+)%2F(.+)

If there really is a need for three seperate expressions, then you may use them like

  1. dl%2F(.+)%2F
  2. dl%2F.+%2F(.+)%2F
  3. dl%2F.+%2F.+%2F(.+)
    or
    .*%2F(.+?)$ (Note: $ marks the end of the input)

OTHER TIPS

Using :

$ perl -lne 'print join "\n", /dl%2F(.*?)(?:%2F)(.*?)(?:%2F)(.*)/' file
ac8ee4c1b1ff6f713d3b19969d6c24e3
52d2eae9
ff2c0ea38304a00b01c3e573babcde109f

Or fetching only one element :

$ perl -lne 'print join "", (/dl%2F(.*?)(?:%2F)(.*?)(?:%2F)(.*)/)[2]' file
ff2c0ea38304a00b01c3e573babcde109f

Or if the delimiter and positions are stable & using :

$ awk -F'%2F' '{print $3, $4, $5}' OFS="\n" file
ac8ee4c1b1ff6f713d3b19969d6c24e3
52d2eae9
ff2c0ea38304a00b01c3e573babcde109f
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top