0

I want to get string 1, string 2 and string 3, ... from below string:

...
BEGINID:"MyId":string 1,string 2,string 3, string ...,ENDID
...

(string 1, string 2 and string 3, ... don't contain charater ,)

Currently, my code is:

Match m = Regex.Match(givenString, "BEGINID:\"MyId\":(.*?)ENDID", RegexOptions.Singleline);
string output = m.Groups[1].Value;

then I split string output by , character.

I want when I regex, I can get all these string I need, without use split, some thing like this pattern:

BEGINID:\"MyId\":(([^,]*,?)*?)ENDID

But I don't know how to do next. Can you tell me? Thank.

EDIT: I just edit my string, so it is general string, not a HTML string.

NoName
  • 7,940
  • 13
  • 56
  • 108
  • 1
    Take a [tour here](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) please – Sriram Sakthivel Nov 29 '14 at 08:26
  • Next you should: use HtmlAgilityPack to parse HTML, read on XPath to find node you are looking for and `String.Split` to split string on comma... Note that it is generally better to not show that you want to [parse HTML with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/) in questions.... – Alexei Levenkov Nov 29 '14 at 08:29

1 Answers1

0

\G assert position at the end of the previous match or the start of the string for the first match.

String input = @"<html>
...
<span id=""MyId"">string 1,string 2,string 3</span>
...
</html>";
Regex rgx = new Regex(@"(?:<span id=""MyId"">|\G),?([^<>,]+)");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[1].Value);

Output:

string 1
string 2
string 3

IDEONE

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • It's my downvote, never recommend wrong tool. Regex is the wrong tool here. Refer comments below question. – Sriram Sakthivel Nov 29 '14 at 08:37
  • @SriramSakthivel then why you fail to mark this question as duplicate of http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ ? – Avinash Raj Nov 29 '14 at 08:42
  • That's not the duplicate, read the question again. Question is different but answer is always same. Duplicates should be marked based on duplicate questions not duplicate answers. – Sriram Sakthivel Nov 29 '14 at 08:44
  • For some reason, I choose to use Regex here. I know some other way to do, example find the ID "MyId", then find next `>` and `<`. Thank for help, sir! I will edit my string to it not be html then people could easy to help me. – NoName Nov 29 '14 at 08:55
  • @TuyenTk Editing the input string may fool people, but it doesn't changes the fact. And that brings XY problem in to place. If you're happy to use regex, go ahead. but be aware of the fundamental problem. – Sriram Sakthivel Nov 29 '14 at 09:08
  • @AvinashRaj it work perfectly, and now I learned a new Regex formula, thank. – NoName Nov 29 '14 at 09:12
  • @SriramSakthivel I will work hard to avoid that problem. I'm quite new to c# and regex. Thank for warning me. – NoName Nov 29 '14 at 09:15