Issue
Important: I have already looked up similar questions on Stackoverflow and elsewhere and I couldnt find a solution.
I'm trying to make a regular expression that matches whatever comes after this pattern (the pattern itself excluded)
[-a-zA-Z0-9]+\/[-\._a-zA-Z0-9]+
I tried using ^
as a NOT operator like so:
[^([-a-zA-Z0-9]+\/[-\._a-zA-Z0-9]+)]
But it throws a syntax error: Unmatched '('
. Seems like it associates the first ]
with the first [
instead of with the second. How to fix this?
I also tried doing a Positive Lookbehind like so:
(?<=([-a-zA-Z0-9]+\/[-\._a-zA-Z0-9]+).*)
But it's not working.
What am I doing wrong?
Solution
You can capture all that follows the match in group 1, and use group 1 in the replacement.
[-a-zA-Z0-9]+\/[-._a-zA-Z0-9]+(.+)
The contents of file to test with:
hello/w0rld/blbalba0
hel-lo/wor_ld?q=0
hello/w0rld)/blbalba0
hel-lo/wor_&ld?q=0(
As you mention in the comments that you want to use sed
:
sed -E 's/[-a-zA-Z0-9]+\/[-._a-zA-Z0-9]+(.+)/\1/' file
Output:
/blbalba0
?q=0
)/blbalba0
&ld?q=0(
Answered By - The fourth bird Answer Checked By - Pedro (WPSolving Volunteer)