J. Doe J. Doe - 18 days ago 6
Ruby Question

How to improve my RegEx to get the text before the pattern?

For example I have the next text:

Hello, my name is %{John}.


Now, I want to take all text before
%{John}
. For this I'm using the next RegEx:

.+?(?=(?<=%{)[^%]*(?=}))


It works, but it includes
%{
this part too. I do not need that. How can I take whole text, but exclude
%{
?

Answer

Try this:

/.+(?=\%\{.*\})/

see http://www.rubular.com/r/YHcuSxMIA8

Comments