0

vb.net regex

I have some strings like this to analyze:

<%JohnSmith$>@ some other text @<%FredJonese%>@ (@<%SallyHarris%>@)@

I need to find occurrences of each block of text that's between <% and %> and also those between each pair of @ symbols.

So for the above example, I want to retrieve the following 6 pieces of text (there is a leading space in the 2nd and 4th lines below):

JohnSmith

some other text

FredJones

(

SallyHarris

)

When I use a patter like "<%\w%> it retrieves the entire line because the line starts and ends with <% and %>

I'm lost on the best way to do this. I need to get the chunks out in the order in which they occur.

Nothing I've tried so far is working. I know I could write a loop that goes through the string character by character but it seems like regex can handle this. Can anyone help out on this?

Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I WANT THIS AND ALL MY QUESTIONS FROM THIS SITE DELETED. THE PEOPLE THAT RUN THIS SITE ARE ARROGANT, NON-HELPFUL, BULLIES THAT WERE LIKELY BULLIED IN JUNIOR HIGH AND FEEL THEY CAN PERFORM EQUALLY CHILDISHLY THINKING THEY HAVE SOME SORT OF IMAGINARY POWER ONLINE. THEY HAVE NO BUSINESS RUNNING A SITE LIKE THIS. DON'T NEED YOU AT ALL. I AM VERY SAD FOR ALL OF YOU. GET A LIFE. –  Dec 26 '14 at 03:34

1 Answers1

0

You need to add the lazy operator after a star or a plus (lazy star or lazy plus). If you don't, the matching is greedy (the default) so it tries to match as much as possible.

<%\w*?%>

Notice the question mark. This will match all three.

steinar
  • 9,383
  • 1
  • 23
  • 37