Reputation: 17486
I have this text:
Name: <% $person %> <& component &>
and i'm trying to come up with a Ruby regex that will give me everything that doesn't look like <% .. %>
or by <& ... &>
.
This is my approach using negative lookahead:
(?!<&|<%)(.+\s*)(?!&>|%>)
but it returns the whole string (tested here), when I expected it to return Name:
.
Upvotes: 1
Views: 505
Reputation: 30971
Try this regex:
(?<=^|[&%]>)
- Positive lookbehind: Either start of text or
a "closing" marker (&>
or %>
).(.+?)
- This is what you want to capture. Note the ?
after +
,
the reluctant version, preventing from "consuming" the whole rest
of text.(?=<[&%]|$)
- Positive lookahead: Either an "opening"
marker (<&
or <%
) or end of text.To sum up:
(?<=^|[&%]>)(.+?)(?=<[&%]|$)
Upvotes: 1
Reputation: 163277
Maybe it is an option for you to replace everything in your string that is between <% .. %>
or <& ... &>
with an empty string:
Upvotes: 2