Reputation: 11
Here's a fun regex problem. Write one regex that will get the results for group one as "path/to/file" for the following example strings:
So to explain, I want to match up to the final forward slash before the first occurrence of an '='.
I was able to match a regex with the example string one (^.*)\/(.*)=
but it captures path/to/file/foo=1 from example string 2 -- this is not as intended, I do not want to see the part of the path with the '='.
I can use (^.*)\/(.*)=(.*)=
to solve example 2, but this doesn't scale to the other examples.
Example 3 is easy enough to capture with (^.*)\/
Being able to match string 3 is a nice to have, but I have a way of easily solving for this in my code.
Thanks for your help and I look forward to learning more about regex.
Upvotes: 0
Views: 198
Reputation: 613
You should use this regex,
^([^=]+)\/
This will match everything except =
and will stop the match as soon as it finds a /
and will capture the contents in group one as you want.
Upvotes: 1