Solved

c# regex Match variable number of strings

Posted on 2014-01-07
2
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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