I have a list of proc names on Linux. Some have slash, some don't. For example,
I need to extract just the proc name without the slash and the rest. I tried a few different ways but still won't get it completely correct. What's wrong with my regex? Any help would be much appreciated.
The problem with the regex is, that the greedy
.+ is going until the end, because everything after it is optional, meaning it is kept as short as possible (essentially empty). To fix this replace the
. with anything but a
works. You can test this regex here. In case it is new to you,
[^\/] matches anything, but a slash., as the
^ in the beginning inverts which characters are matched.
Alternatively, you can also use
split as suggested by Moses Koledoye.
split is often better for simple string manipulation, while regex enables you to perform very complex tasks with rather little code.