Solved

c# regex Match variable number of strings

Posted on 2014-01-07
2
683 Views
Last Modified: 2014-01-08
Hi,
The following regex code will match two strings which fall in between ">" and "<":
--------
string rawReview = "aaaaaaaa>string1<bbbbbbb>string2<cccccccc";
           
match = Regex.Match(rawReview, @">([^<]*).*?>([^<]*)");
if (match.Success)
{
    Console.Write("Match found  {0} {1}\n", match.Groups[1], match.Groups[2]);
}
--------
Match found string1 string2

I would like to extend this example to be able to match an arbitrary number of strings which fall between the bracket chars.  Is this possible?  

Thank you very much for reading and considering my question.
0
Comment
Question by:97WideGlide
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39764616
Try this:
            match = Regex.Match(rawReview, @">([^<]*)");
            while (match.Success) {
                Console.Write("Match found  {0}\n", match.Groups[1]);
                match = match.NextMatch();
            }

Open in new window

0
 
LVL 8

Author Closing Comment

by:97WideGlide
ID: 39765096
Simple when you know what you're doing.  I solved it last night after two hours using a MatchCollection which clearly wasn't necessary.  Your solution taught me a bit about regular expressions.

Thank you very much.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
haveThree challenge 22 118
dividesSelf challange 15 135
matchUp  challenge 9 108
How do sunrise and sunset times change with altitude 14 30
Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question