The regex should match
/exampleline - match examplelineexampleline - match examplelineexampleline/ - match exampleline/exampleline/ - match examplelineI tried ?\/(.+)\/? but it didn't work
/exampleline/ and exampleline/ matched exampleline/, instead of exampleline
It is necessary to remove the symbol "/" from the group. Try this:
\/?([^\/]+)\/?
You can match any character except /. Then optionally match a part that matches the whole line ending on a character other than /
[^/\n](?:.*[^/\n])?
The pattern matches:
[^/\n] Match a character other than a newline or /(?: Non capture group to make the whole part optional
.* Match the whole line[^/\n] Match a character other than a newline or /)? Close the non capture group and make it optionalSee a regex demo
If the match should start and end with a non whitespace character, you can replace the \n with \s
[^/\s](?:.*[^/\s])?
See another regex demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With