|Keep It Simple, Stupid|
Re: matching substring in urlby roboticus (Chancellor)
|on Nov 09, 2019 at 19:11 UTC||Need Help??|
Yet another way you could do it:echo "$XXX" | perl -pe '$_=(split "/", (split ";"))[-1]'
It might look a little magical at first, but it's actually pretty simple. Instead of using a regular expression, I first split the string on ";" and kept only the first bit ((split ";")) to chop off the optional bits. Then I split that first bit on "/" and kept the last part ((split "/", ...)[-1]) to get the value you wanted. Finally, I assigned the result to $_ and used the -p option to print the value.
I got there essentially by starting with something like this:
Since you can extract a chunk of a list by subscripting it, I first contracted the above to:
Then I wanted to get rid of the temporary array by keeping *only* the final result by again subscripting the resulting list:
Then to turn it into a one liner, I changed it to this:
Oops! The parser sees "print (" and thinks we're wrapping the print arguments in parenthesis, leading to a syntax error later. I could've fixed the problem by putting a unary "+" on the thing to print, as that tells perl that the parenthesis isn't part of print:
But I went a different route: since you're using echo to inject the value into perl as the value $_, I instead elected to use the -p option which tells perl to print the value in $_ after the code executes, giving me the bit of code I suggested.
When your only tool is a hammer, all problems look like your thumb.