1

I want to split a string into parts based on a regex (\$\d+\$), but I also want to know what the value of these split-points is. So for example, if have the string:

<option value="beholder" selected="selected">$33685$</option>
<option value="gnuchess_fancy">$33687$</option>
<option value="gnuchess_simple">$33689$</option>
 | $29000$
<option value="beholder">$33671$</option>
<option value="gnuchess_fancy">$33673$</option>

I want to split it such that the results seperate parts become:

o <option value="beholder" selected="selected">
o $33685$
o </option><option value="gnuchess_fancy">
o $33687$
o ......

Splitting with the regex \$\d+\$ gives me only the first and third item of the above list, while I want all items.

Programming language does ofcourse not matter, it's about the regex and how to split (or match).

I also tried to match with the following regexs, but no luck

\$\d+\$|.*?
.*?|\$\d+\$
.*?\$\d+\$.*?
(.*?|\$\d+\$)*

Any help is greatly appreciated.

Henri
  • 5,065
  • 23
  • 24
  • 1
    *Programming language does ofcourse not matter* - It does matter. The syntax is different in each language. What language are you using? Or do you want people to suggest a language? – Mark Byers Nov 09 '10 at 10:42
  • Before someone else writes it, read this answer: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Sean Patrick Floyd Nov 09 '10 at 10:45
  • @Mark, My bad in that case, I use it in combination with .NET. – Henri Nov 09 '10 at 11:01
  • @seanizer, I'm not trying to parse HTML with a regex :) thnx for the link though – Henri Nov 09 '10 at 11:02
  • sure you are. the string is HTML, and you are trying to split it using Regex. – Sean Patrick Floyd Nov 09 '10 at 11:14

1 Answers1

1

Try splitting with parentheses:

(\$\d+\$)

On some languages, the captured group are added to the result array.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Thanks! Already checked your tags - on .net it should work as-is. On php you should add the `PREG_SPLIT_DELIM_CAPTURE` flag. – Kobi Nov 09 '10 at 11:00