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.

5
  • 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? Commented Nov 9, 2010 at 10:42
  • Before someone else writes it, read this answer: stackoverflow.com/questions/1732348/… Commented Nov 9, 2010 at 10:45
  • @Mark, My bad in that case, I use it in combination with .NET. Commented Nov 9, 2010 at 11:01
  • @seanizer, I'm not trying to parse HTML with a regex :) thnx for the link though Commented Nov 9, 2010 at 11:02
  • sure you are. the string is HTML, and you are trying to split it using Regex. Commented Nov 9, 2010 at 11:14

1 Answer 1

1

Try splitting with parentheses:

(\$\d+\$)

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Already checked your tags - on .net it should work as-is. On php you should add the PREG_SPLIT_DELIM_CAPTURE flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.