Reputation:
I want to get the last string between special characters. I've done for square bracket as \[(.*)\]$
But, when I use it on something like Blah [Hi]How is this[KoTuWa]
. I get the result as [Hi]How is this[KoTuWa]
.
How do i modify it to get the last stringthat is KotuWa
.
Also, I would like to generalise to general special characters, instead of just matching the string between square brackets as above.
Thanks, Sai
Upvotes: 2
Views: 87
Reputation: 40526
You can require that the string between brackets does not contain brackets:
Edit: thanks to funkwurm and jcubic for pointing out an error. Here's the fixed expression:
\[([^[]+)\][^\[]*$
If you need to use other separators than brackets, you should:
\[
and \]
with your new separatorsFor example, assuming you need to use the separators <>
instead of []
, you'd do this:
<([^<]+)>[^\>]*$
Upvotes: 0
Reputation: 4659
I would do this:
[^[\]]+(?=][^[\]]*$)
To extend this to other types of brackets/special chars, say I also wanna match curly braces {
and double quotes "
:
[^{}"[\]]+(?=["\]}][^{}"[\]]*$)
Debuggex Demo (I added the multi-line /m
only to show multiple examples)
Upvotes: 4