0

let's say i have this line string:

'''Sir Winston Leonard Spencer-Churchill''', {{Post-nominals|post-noms=[[Knight of the Garter|KG]]

i already got to use string split with no empty results in my code, but i want now the result for that string will be:

[0] "'''"
[1] "Sir Winston Leonard Spencer-Churchill"
[2] "'''"
[3] ", "
[4] "{{"
[5] "Post-nominals|post-noms="
[6] "[["
[7] "Knight of the Garter|KG"
[8] "]]"

and so on, so basicaly my delimeters are {"'''","{{","}}","[[","]]"} just need the way for it, i'll edit that later. if it won't be with regex it'll be better.

Servy
  • 202,030
  • 26
  • 332
  • 449
k-man
  • 1,121
  • 5
  • 17
  • 26
  • Why don't you want to use regex? – Servy Jan 10 '13 at 17:12
  • @Servy regex.Split also removes the delimiters – Sten Petrov Jan 10 '13 at 17:13
  • Normally it would be duplicate of http://stackoverflow.com/questions/2910536/regex-split-string-but-keep-separators, but you explicitly want other solution... – Alexei Levenkov Jan 10 '13 at 17:15
  • @StenPetrov Yep, as will most other splitting mechanisms. But if someone comes up with a solution to the problem, as asked, and uses regex, I fail to see the problem. Obviously the problem cannot be solved by just calling `Split` (whether it's from Regex or not) and passing in the provided delimiters. – Servy Jan 10 '13 at 17:16
  • yeah. i saw already that post, anyway i asked without Regex because i can't realy understand the sytax of them. anyway please if someone have good solution with Regex and can explain me how to edit that i'll be also glad to try, right now i'm not sure those are the only delimeters i'm going to work with.. thanks – k-man Jan 10 '13 at 17:24
  • 2
    @k-man the "unknown" is where the answers are, you can come up with some clunky way of doing it instead of spending a couple hours learning a nice new technique; months later someone will look at your code, possibly even you, and wonder what the heck was done here. The regex in 2kay's answer (+1'd) below is really simple, basically your separators and pipes between them - doesn't get simpler than 3 lines of code. – Sten Petrov Jan 10 '13 at 17:31
  • @Sten Petrov you right and i know it's a bit putting my head in the ground, i have this huge assignment to be submitted soon and can't realy wide my knowledge right now, it's just one type of line out of many in a parser that indexes wiki pages that later i'll have to retrieve results from. – k-man Jan 10 '13 at 17:55
  • @k-man you've been on this site for 2 years, so you're not that new to programming. All these "I'm in a hurry" excuses only cause you to develop bad habits of writing archaic code and doing so takes a lot longer than learning the tools at your disposal. Lincoln said once he'd sharpen the axe for 4 hours if he had 6 to chop down a tree and he was right. – Sten Petrov Jan 10 '13 at 18:20

2 Answers2

3

Using Regex:

string input = "'''Sir Winston Leonard Spencer-Churchill''', {{Post-nominals|post-noms=[[Knight of the Garter|KG]]";
string pattern = @"('''|\{\{|\}\}|\[\[|\]\])";
string[] result = Regex.Split(input, pattern);
tukaef
  • 9,074
  • 4
  • 29
  • 45
0

use this regex (?<=^?'''|\{\{|\[\[|\]\]|\}\})(.+?)(?=$?'''|\{\{|\[\[|\]\]|\}\})

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52