开发者

vb.net regex finding blocks of text between <@ and @> [duplicate]

开发者 https://www.devze.com 2023-02-18 07:44 出处:网络
This question already has answers here: Get text between two strings Regex VB.Net (5 answers) Closed 3 years ago.
This question already has answers here: Get text between two strings Regex VB.Net (5 answers) Closed 3 years ago.

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 &开发者_Go百科lt;% 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消